I have the following URL:
http://example.com/pages/cms/impressum.php
and want to get an URL like this:
http://example.com/impressum
My rewrite Rule is:
RewriteEngine on
RewriteBase /
RewriteRule ^impressum$ /pages/cms/impressum.php [R,L]
The Problem is, I can open the url in both ways. I would like a forwarding from /pages/cms/impressum.php to /impressum. If I use [R=301], the second URL which I do not want works. I want to reverse this rule.
Use:
RewriteEngine On
RewriteBase /
# To externally redirect pages/cms/impressum.php to impressum
RewriteCond %{THE_REQUEST} \s/+pages/cms/impressum\.php[\s?] [NC]
RewriteRule ^ /impressum [R=301,L]
# To internally forward impressum to pages/cms/impressum.php
RewriteRule ^impressum/?$ pages/cms/impressum.php [L,NC]
Related
I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]
I have a link like this:
/add.php?id=50&link=page2
I want it to redirect to this:
/add/50/page2
How can I do this? I tried the following, but it did not work:
Redirect /add.php?id=50&link=page2 /add/50/page2
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /add\.php\?id=([^\s&]+)&link=([^\s&]+) [NC]
RewriteRule ^ add/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^add/([^/.]+)/([^/.]+)/?$ add.php?id=$1&link=$2 [L,QSA,NC]
What I am trying to do:
Change
www.mysitename.com/pages/about
to
www.mysitename.com/about
What I have tried so far:
RewriteEngine on
RewriteRule ^(.*)$ pages/$1
but when I go onto my website and click the about section, the url is still www.**.com/pages/about. htaccess is enabled on my server, so that's not the problem.
What am I doing wrong?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# change URL in browser from /pages/about to /about
RewriteCond %{THE_REQUEST} \s/+pages/(\S*) [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# internally forward /about to /pages/about
RewriteRule ^((?!pages/).+)$ pages/$1 [L,NC]
I'm having issues trying to do a 301 redirect in my htaccess file. It is having a problem working with a part of my rewrite rules I have created. In summary, the rewrite strips the extension off of the page and replaces the query ?primary-view=city to /city. Here is my htaccess file.
RewriteEngine On
# Redirect /page.php to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /page to /page.php
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC]
# Redirect /page.php?primary-view=value to /page/value
RewriteCond %{THE_REQUEST} \s/+(page)\.php\?primary-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
# Redirect /chicago-downtown to /chicago
Redirect 301 /chicago-downtown to /chicago
So the url after the rewrite will look something like
#Original url
example.com/page.php?primary-view=chicago-downtown
#Url rewritten properly
example.com/page/chicago
If I type in example.com/page/chicago then it works fine. If I type in example.com/page/chicago-downtown its currently redirecting to the following:
#Current bad redirect from example.com/page/chicago-downtown
example.com/page/chicago?primary-view=chicago-downtown
#Desired redirect from example.com/page/chicago-downtown
example.com/page/chicago
I have tried man things and came to the conclusion that it is the following line causing the issue with the 301 redirect.
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
Is there some way I can rewrite my rewrite rules, or is there a rewrite condition I need to use to prevent this from happening?
You don't need Redirect rule. Just an additional mod_rewrite rule for chicago-downtown => chicago:
RewriteEngine On
RewriteRule ^(page/chicago)-downtown/?$ /$1 [L,R=302,NC]
# Redirect /page.php to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /page to /page.php
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC]
# Redirect /page.php?primary-view=value to /page/value
RewriteCond %{THE_REQUEST} \s/+(page)\.php\?primary-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
This is probably caused because you're mixing mod_alias directives with mod_rewrite directives. That means both directives get applied to the same request in an unwanted way. Remove the Redirect directive (mod_alias) and user a rewrite rule instead. But you need to add the rule before your other rules (i.e. right after RewriteEngine On)
RewriteRule ^chicago-downtown(.*)$ /chicago$1 [L,R=301]
OK so... I have the following URL which works on my site:
http://my_domain.net/w/mRD3nKkM
The rewrite for this in the root of my site is:
RewriteRule ^([w])/(\w+)$ res/$1/response.php?id=$2 [L]
Simple stuff and works a treat. Now I want to redirect any traffic that hits that URL unencrypted to go over https, like below:
https://my_domain.net/w/mRD3nKkM
So I put another .htaccess file within the res/w/ folder conatining the following:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} ^/response.php$
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^(.*)$ https://my_domain.net/w/$1 [R,L]
To my miind this should work, but doesn't.
To be clear, I have the following URL rewrite working:
http://my_domain.net/w/mRD3nKkM
and I would like it to look like this:
https://my_domain.net/w/mRD3nKkM
Thanks
You should handle http => https before doing internal forward stuff. Following should work for you:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^w/.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
RewriteRule ^(w)/(\w+)/?$ res/$1/response.php?id=$2 [L,NC]