I'm trying to achieve the following with .htaccess:
domain.tld/ displays the content originally at domain.tld/home/
if you visit domain.tld/home/, you are redirected to domain.tld/ (to avoid duplicate content)
I tried the following, but it resulted in an infinite loop:
RewriteEngine on
RewriteRule ^$ /home/ [L]
RedirectMatch 301 ^/home/$ /
Any ideas? Thank you.
Found the solution:
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /+home/([^\?\ ]*)
RewriteRule ^ /%1 [R=301,L]
RewriteRule ^$ /home/ [L]
Based on: How to avoid duplicate content mapping urls with htaccess
Related
I have URLs with two query parameters e.g. /skills/keywords/list.php?industry=retail&q=analyst and I'd like to redirect those URLs to /list-of-%2-skills-in-%1 .
I have tried the code below which looks at the query "industry" and "q" to build the destination URL: /list-of-%2-skills-in-%1
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^industry=(.*)&q=(.*)$ [NC]
RewriteRule ^skills/keywords/list\.php$ /list-of-%2-skills-in-%1? [L,R=301]
I'm expecting: /skills/keywords/list.php?industry=retail&q=analyst to redirect to /list-of-analyst-skills-in-retail which it does but I have a 404 content not found.
Can someone point me to the right direction please?
This is because the destination path /list-of-%2-skills-in-%1 doesn't exist on your server. You need to rewrite this path to the existent file /skills/keywords/list.php?industry=retail&q=analyst .
RewriteEngine On
RewriteBase /
#redirect /skills/keywords/list.php?industry=foo&q=bar
#to /list-of-bar-skills-in-foo
RewriteCond %{THE_REQUEST} /skills/keywords/list\.php\?industry=([^&]*)&q=([^\s]+) [NC]
RewriteRule ^skills/keywords/list\.php$ /list-of-%2-skills-in-%1? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^list-of-([^-]+)-skills-in-([^-]+)/?$ /skills/keywords/list.php?industry=$1&q=$2 [NC,L]
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]
Alright so I have done the process of cleaning up my urls and everything is going good I put the following in my htaccess.
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /l/index.php?mp3=$1&img=http://www.dailynewsounds.com/wp-content/uploads/2016/01/tinashe-5535799554a1b-3.jpg&g=R\%26amp\%3BB&p=8\%20hours\%20ago&s=+Energy&a=Tinashe+Ft.+Juicy+J+ [L]
And this works to change my urls but we have our nasty urls directing to the pages.
http://dl.example.com/l/index.php?mp3=oo8aey7qw3n9&img=http://www.example.com/wp-content/uploads/2016/01/stuey-rock-ft-sy-ari-da-kid-569e83c69cf5e.jpeg&g=R%26amp%3BB&p=10%20hours%20ago&s=+Me+And+You&a=Stuey+Rock+Ft.+Sy+Ari+Da+Kid+
How can I make it so that I can check my url to see if it contains index.php and if it does redirect it to the new page?
Try the following rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /l/index\.php\?\S*mp3=\S* [NC]
RewriteCond %{QUERY_STRING} \bmp3=([^\s&]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
If you're using Apache 2.4.x, then replace the L] with L,QSD].
I have link like this:
www.site.com/page.php?p=1
Need to rewrite it to friendly URLs in htaccess
RewriteRule ^home$ page.php?p=1
It works but now I have two active links with the same content.
Tried to add 301 redirect from old link to new but stuck in loop. Any ideas how to fix that?
Try matching against the actual request so that your rules won't loop:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page\.php\?p=1(&|\ |^)([^\ ]*)
RewriteRule ^page\.php$ /home?%3 [L,R=301]
# then your internal rewrite
RewriteRule ^home$ page.php?p=1
Remove the redirect on the page and handle it in the htaccess.
RewriteRule ^page\.php\?p=1$ /home [L,R=301]
This will redirect to /home and stop the redirect loop you have now.
another quick & dirty way to prevent looping in these situations i've found is to add a querystring and then check for its existence in the redirect.
RewriteCond %{QUERY_STRING} ^p=1
RewriteCond %{QUERY_STRING} !foo=bar
RewriteRule ^page\.php$ /home [NC,R=301,L]
RewriteRule ^home$ page.php?p=1&foo=bar [NC,L]
found on this site: http://answers.oreilly.com/topic/542-how-to-properly-redirect-for-maximum-seo/
Redirect 301 /page.php?p=1 www.yourwebsite.com/home?p=1 RewriteRule
^home?p=1$ /page.php?p=1
I want to hide any request on the following php file to www.example.com/.
www.example.com/index.php to www.example.com/ (hide index.php)
www.example.com/content.php to www.example.com/ (hide content.php)
www.example.com/welcome.php to www.example.com/ (hide welcome.php)
Note: I just want to hide the file without redirecting them. For example, hide the part on index.php or content.php without redirect them to www.example.com/.
I tried
RewriteEngine On
RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
I also tried
DirectoryIndex index.php
DirectoryIndex content.php
DirectoryIndex welcome.php
These code didn't seem to work. Any idea? Sorry, i'm very new to this.
If I recall correctly something like this should work correctly.
RewriteRule ^/index.php$ http://www.example.com/ [R,NC,L]
RewriteRule ^/content.php$ http://www.example.com/ [R,NC,L]
RewriteRule ^/welcome.php$ http://www.example.com/ [R,NC,L]
Kind regards,
Bo