Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

regex - how to remove folder name from url using htaccess

I want to change the URL from:

http://domain.com/Portfolios/iPhone/app

To:

http://domain.com/iPhone/app

And same for all URLs like:

domain.com/Portfolios/iPad/app

To:

domain.com/iPad/app

And from:

domain.com/Portfolios/xyz/app

To:

domain.com/xyz/app

I have tried a lot but nothing is working for me, so please help me.

When I need help why any buddy down voting it, if you know the answer then answer it.

Update

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^Portfolios(/.*|)$ $1 [L,NC]  
</IfModule>
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^Portfolios/(.*)$ /$1 [L,NC,R]

Explanation: Above rules is matching URL patter that starts with Portfolios and have somthing like /Portfolios/xyz/app and puts xyz/app in $1. It makes an external redirection to /$1 i.e. /xyz/app.

These are the flags used:

L  - Last
NC - Ignore (No) Case comparison
R  - External redirection (with 302) -- can be changed to R=301

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...