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>
E_net4
  • 27,810
  • 13
  • 101
  • 139
anytime
  • 409
  • 3
  • 7
  • 14
  • 1
    It might help to know that redirects can be cached by the browser, I only just learned it after some hours; https://stackoverflow.com/questions/4499541/htaccess-file-somehow-being-cached – wkille Nov 24 '20 at 13:51
  • 1
    See this `Remove folder name from URLs` https://helponnet.com/2021/11/30/remove-folder-name-from-url/ – Amit Verma Feb 11 '22 at 05:25

2 Answers2

37

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    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 Sep 24 '13 at 05:01
  • I have tried url: http://domain.com/Portfolios/iPad/app and htaccess placed in root – anytime Sep 24 '13 at 05:05
  • 1
    @anytime: I believe `Portfolios` is a folder directly below DOCUMENT_ROOT is that correct? – anubhava Sep 24 '13 at 05:10
  • 1
    @anubhava yes buddy, n thanks for your help :) , I want to enable both the url like:http://domain.com/Portfolios & http://domain.com/iPhone/app (redirect from http://domain.com/Portfolios/iPhone/app) – anytime Sep 24 '13 at 05:13
  • 1
    @anytime: Can you try this rule: `RewriteRule ^Portfolios/(.*)$ /$1 [L,NC,R]` and let me know. – anubhava Sep 24 '13 at 05:17
  • anubhava, [**I insert your code in my `.htaccess`**](https://github.com/Kristinita/Kristinita.github.io/blob/master/.htaccess#L16-L21) → I create [**Kristinita.github.io/Portfolios/bar/app.html**](https://github.com/Kristinita/Kristinita.github.io/blob/master/Portfolios/bar/app.html) file. If `http://kristinita.github.io/Portfolios/bar/app.html`, `app.html` was opened for me; but if `http://kristinita.github.io/bar/app.html`, I get 404 error. What I do wrong? Thanks. – Саша Черных Jan 25 '17 at 18:24
  • As you can see your 2nd URL doesn't even have `Portfolios/` at the start so this rule won't redirect. – anubhava Jan 25 '17 at 18:47
  • 1
    @anubhava, all right. Now [**http://kristinita.github.io/Portfolios/app.html**](http://kristinita.github.io/bar/app.html) successfully open for me, but [**http://kristinita.github.io/bar/app.html**](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**](http://kristinita.github.io/bar/app.html) file was open. How to do it? Thanks. – Саша Черных Jan 30 '17 at 08:35
  • @СашаЧерных: OP just asked for removal of `/Portfolios/` from every URL. You seem to be having a different problem. Open a new question with all the details and I will try to answer. – anubhava Jan 30 '17 at 15:27
  • What does this do `RewriteBase /` –  Oct 19 '17 at 00:26
  • @anubhava I am using your example as `RewriteBase /coronavirus/dashboard/` `RewriteRule ^region/(.*)$ /$1 [L,NC,R]` to remove `/region` from https://quarantine.country/coronavirus/dashboard/region/usa/ but can't get it to work. What am I missing? Thank you! – Yatko May 20 '20 at 12:48
  • @Yatko: Can you please post a separate question with all details and I will try to post a solution. – anubhava May 20 '20 at 14:01
  • @anubhava thank you! Please see the question here: https://stackoverflow.com/questions/61916016/htaccess-url-rewrite-remove-folder-from-url – Yatko May 20 '20 at 14:45
  • 1
    oh nvm I guess the question was different than I expected, removed my comment – Flash Thunder Jun 21 '22 at 09:50
  • can't change it – Flash Thunder Jun 21 '22 at 10:49
  • 1
    ok, done, sorry. – Flash Thunder Jun 21 '22 at 11:40
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/ >

Digant
  • 398
  • 3
  • 10