how to remove folder name from url using htaccess - regex

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>

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.

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/ >

Related

mod rewrite path in url

I would like to rewrite with the .htaccess-file in root directory the internal path example.com/app/app-v1/ (including all subdirectories) to example.com/app/in the url, so that everything in the /app-v1/ folder is right behind the /app/ directory in the url.
I tried
RewriteEngine On
Redirect /app/app-v1/ /app/
but that's not working
You may try this code in site root .htaccess:
RewriteEngine On
RewriteRule ^app/((?!app-v1/).*)$ app/app-v1/$1 [NC,L]
(?!app-v1/) is a negative lookahead to avoid rewriting URI starting with /app/app-v1/ to this rule.

RedirectRule with .htaccess

I am new to the .htaccess file.
I want to make pretty URLs but the server always gives me 404 or 500 errors.
For example, I want to redirect
http://www.example.com/dir1
to
http://www.example.com/dir1/file1.html
without showing file1.html in the address bar.
I've tried
RedirectRule /dir1/$ /dir1/file1.html but the server says 404.
The .htaccess is in root.
What should I do?
Remember that .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RedirectRule ^dir1/?$ dir1/file1.html [L,NC]
OR use this rule in /dir1/.htaccess:
RewriteEngine On
RewriteBase /dir1/
RedirectRule ^/?$ file1.html [L]
This htaccess rule will do the trick.
RewriteEngine On
RewriteBase /
RedirectRule ^dir1/?$ dir1/file1.html [L]
For the record the terminating /? means that you to redirect links with /dir1 and links with /dir1/

Is there a way to rewrite just a directory, so that all articles within that directory redirect 1 level up?

I have a site http://www.mysite.com/blog/category/post/ and I need to redirect it to http://www.mysite.com/category/post ie, I want to remove the blog from my URL. Is there a way to do it using permanent redirects in .htaccess?
Thank you very much!
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 ^blog(/.*|)$ $1 [L,R=301,NC]

.htaccess regex for HTTP_HOST followed by a specific folder

I'm trying to edit my .htaccess file to do the following:
1) Determine if the HTTP_HOST is not my own domain, example.com
2) Determine if it is otherdomain.com/admin/_anything_here_
/admin/ is the specific folder I am looking for, then I don't care what comes after it (but I do need to keep it there). So basically anything that has /admin/ as the first folder and isn't example.com should rewrite to otherdomain.com/admin/_anything_still_here_
It's my understanding that using (.*)$ will "store" the _anything_here_ part and allow me to use it with $1.
I have this so far but it isn't working fully:
RewriteCond ^%{HTTP_HOST}+(/admin.*)$ !^([^.]+\.)*example\.com+(/admin.*)
RewriteRule (.*)$ rewrite/to/here$1/ [L]
Try adding the following to the .htaccess file in the root directory of your site.
You need to replace somefolder with the actual folder you want to rewrite to
RewriteEngine on
RewriteBase /
#if its not example.com
RewriteCond %{HTTP_HOST} !example\.com$ [NC]
#if its admin folder, rewrite it to somefolder
RewriteRule ^(admin/.*)$ somefolder/$1[L,NC]
The above assumes that otherdomain.com and example.com are on the same server with the same root folder.

How to convert text to lowercase URLs using .htaccess

I want to set up 301 redirects in my .htaccess file so URLs like
http://example.com/Foo
http://example.com/Foo/Bar
http://example.com/Foo/Bar/Blah
change to
http://example.com/products/foo
http://example.com/products/foo/bar
http://example.com/products/foo/bar/blah
There are a discrete number of "Foo" cases which I can target with RewriteRule ^Foo, but how to append the "products" part?
First add this line in <VirtualHost> section OR at the end of your httpd.conf file:
RewriteMap lc int:tolower
Then have these rules in .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteRule ^(Foo.*)$ /products/${lc:$1} [R=301,L]
R=301 for sending back 301 to browser
L for marking it last rule