23

I want to change the URL from:

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

To:

http://example.com/iPhone/app

And same for all URLs like:

example.com/Portfolios/iPad/app

To:

example.com/iPad/app

And from:

example.com/Portfolios/xyz/app

To:

example.com/xyz/app

I have tried a lot but nothing is working for me.


<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^Portfolios(/.*|)$ $1 [L,NC]  
</IfModule>
1
  • 1
    It might help to know that redirects can be cached by the browser, I only just learned it after some hours; stackoverflow.com/questions/4499541/… Commented Nov 24, 2020 at 13:51

2 Answers 2

38

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

RewriteEngine On

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

Explanation: Above rule is matching URL pattern 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 Rule
NC - Ignore (No) Case comparison
R  - External redirection (with 302)

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Sign up to request clarification or add additional context in comments.

16 Comments

when I tried RewriteRule ^Portfolios(/.*|)$ $1 [L,NC] there are no effects, when I tried RewriteRule ^Portfolios(/.*|)$ $1 [L,NC,R=301] Its redirecting with folder structure C:/wamp/www/project...
@anytime: I believe Portfolios is a folder directly below DOCUMENT_ROOT is that correct?
@anubhava yes buddy, n thanks for your help :) , I want to enable both the url like:domain.com/Portfolios & domain.com/iPhone/app (redirect from domain.com/Portfolios/iPhone/app)
@anytime: Can you try this rule: RewriteRule ^Portfolios/(.*)$ /$1 [L,NC,R] and let me know.
@anubhava, all right. Now http://kristinita.github.io/Portfolios/app.html successfully open for me, but http://kristinita.github.io/bar/app.html — 404 error. But the author of the question asked on the contrary — that http://kristinita.github.io/bar/app.html file was open. How to do it? Thanks.
|
0

You can also set your root directory as /var/www/Portfolios instead of /var/www/ in /etc/apache2/sites-enabled by writing DocumentRoot line as

DocumentRoot /var/www/Portfolios

instead of DocumentRoot /var/www/ and also this line < Directory /var/www/ > changed to

< Directory /var/www/Portfolios/ >

1 Comment

I get Forbidden: You don't have permission to access this resource. after doing this!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.