htaccess rewrite pretty url without changing rewritebase - apache

Attempting to rewrite a few different URLs without changing the RewriteBase. The following URLs with queries:
http://example.com/directory1/directory2/job?id=100
http://example.com/directory3/directory4/profile?id=200
would become:
http://example.com/directory1/directory2/job/100
http://example.com/directory3/directory4/profile/200
Currently rules:
RewriteRule ^directory1/directory2/job/([0-9]+)$ ./directory1/directory2/job?id=$1 [L,QSA]
RewriteRule ^directory3/directory4/profile/([0-9]+)$ ./directory3/directory4/profile?id=$1 [L,QSA]
Full htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
Options +FollowSymLinks
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteRule ^directory1/directory2/job/([0-9]+)$ ./directory1/directory2/job?id=$1 [L,QSA]
RewriteRule ^directory3/directory4/profile/([0-9]+)$ ./directory3/directory4/profile?id=$1 [L,QSA]
</IfModule>

Have your rules in this order:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
Options +FollowSymLinks
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} (/directory1/directory2/job)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} (/directory3/directory4/profile)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
RewriteRule ^(directory1/directory2/job)/([0-9]+)$ $1?id=$2 [L,QSA]
RewriteRule ^(directory3/directory4/profile)/([0-9]+)$ $1?id=$2 [L,QSA]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Related

How to redirect a parameter to a subfolder with .htaccess?

How to redirect from:
https://example.com/blog/?p=title-of-blog-post
to:
https://example.com/blog/title-of-blog-post
In the /blog/ folder, I have the following .htaccess file:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
</IfModule>
I have tried into the .htaccess of the root, the following code, but it returs 404 Not found:
.htaccess in /public_html/
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/blog/\?p=([0-9]*)\s [NC]
RewriteRule . blog/%1? [R=301,L]
RewriteRule ^blog/([0-9]+)$ blog/?p=$1 [L]
You must use new rule in blog/.htaccess only. Here is suggested code:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{THE_REQUEST} \s/blog/\?p=([\w-]+)\s [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ ?p=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

htaccess trailing slashes just for one URL

Having the following .htaccess rules
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^dashboard/(.*)$ back/index.html [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I want to redirect all incomings requests if they match /dashboard to end with a trailing slash. But only for this case.
if I use
RewriteRule ^dashboard/(.*)$ back/index.html [L]
RewriteCond %{REQUEST_URI}dashboard /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
will fail because all my URL's are becoming a trailing slash.
How can I have a trailing slash only for this case?
Have it this way:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^dashboard/(.*)$ back/index.html [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# add / after /dashboard
RewriteRule ^dashboard$ $0/ [L,R=301,NC]
# remove / from all other URIs
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule !^dashboard/ %1 [L,R=301,NC]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Make sure to test it after completely clearing browser cache.

htaccess non-www to www only redirects to index

The following is only redirecting to the index. It doesn't preserve urls. e.g. www.mysite.com/anotherpage redirects to mysite.com/index.php
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Full htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>

mod_rewrite with three parameters

My URL:
localhost/categories?DPt=MQ==&CTg=MQ==&NLs=Mw==
I want so:
localhost/product-a/departure-a/categories-a/
My htaccess:
<IfModule mod_rewrite.c>
RewriteRule ^confirmation/?$ confirmation.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]+)/?$ products.php?PDt2=$1 [QSA,NC,L]
RewriteRule ^/?categories/([^/]+)/?$ categories.php?DPt=$1&CTg=$2&NLs=$3 [QSA,NC,L]
</IfModule>
But it does not.
What is wrong?
This is my version of .htaccess with comments inside:
<IfModule mod_rewrite.c>
# stop rewrite if matches existing file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* - [L]
# redirect trailing slash
RewriteRule ^(.+)/$ /$1 [R=301,L,QSA,NE]
# confirmation
RewriteRule ^confirmation$ /confirmation.php [L]
# match everything except slash to product
RewriteRule ^([^/]+)$ /products.php?PDt2=$1 [QSA,L]
# match categories
RewriteRule ^(.+)/(.+)/(.+)$ /categories.php?DPt=$1&CTg=$2&NLs=$3 [QSA,L]
</IfModule>

.htaccess rewrite - redirect pages

I would like to know how could I redirect all visitor who is accessing the following addresses:
https://example.com/anypage to https://example.com/search/anypage
It cannot redirect people who is already at https://example.com/search/anypage to avoid loop. This is my current .htaccess:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# the following is for rewritting under FastCGI
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Thanks
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !/search/ [NC]
RewriteRule ^(.*)$ search/$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# the following is for rewritting under FastCGI
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>