I want to change my website's dynamic urls to Search Engine Friendly URL's
Now the urls like this www.website.com/news.php?id=127591 ,
I want it became this www.website.com/news/127591/this-is-article-subject
I added this
RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT]
in my .htaccess file. Everything from /news.php?id=123 change to /new/123/this-is-article-subject
The problem is, now I have two links refer to the same contents. Both /news.php?id=123 and /new/123/this-is-article-subject are the exactly duplicate content
It is said that search engine will punish this if they found duplicated contents.
I check the answers online and found this,
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
HTTP 301 permanent redirect from the old URL to the new URL.
But this still have problem. When I put those three lines together, it not works.
RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
I guess the reason is the recursive loop. How could I solve this kind of problem?
Thanks!
Update
I changed to this
RewriteRule ^news/([0-9]+) /news.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
None of the two url work.
Please try this rule in your .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteCond %{THE_REQUEST} \?id=([0-9]+)\s [NC]
RewriteRule ^news\.php /news/%1? [R=301,L]
RewriteRule ^news/([0-9]+) /news.php?id=$1 [L,NS,NE,QSA,NC]
UPDATE:: Based on your comments:
Inside news.php when URL doesn't have /some-title then output this META tag to stop indexing /news/987 type URIs:
<meta name="robots" content="NOINDEX, NOFOLLOW">
Once you notice URI of /news/987/some-title inside news.php simply mask above META tag.
I have tested it and seems to be working fine so let me know if doesn't work for you.
You need to inspect the URI in the HTTP request line (i.e. %{THE_REQUEST}) as the other could already have been rewritten (like in your case):
RewriteCond %{THE_REQUEST} ^GET\ /news\.php\?
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
If you're only concerned about search engines, you could create a robots.txt file containing:
User-agent: *
Disallow: /news.php
This will make sure that search engines don't follow the news.php links.
To fix your rewrite rules, you might try adding L to the first RewriteRule to make sure that mod_rewrite doesn't continue:
RewriteRule ^news/([0-9]+) /news.php?id=$1 [PT,L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^news\.php /news/%1? [R=301,L]
try using L
[L]
instead of
[PT]
If [L] doesn't work for you, the issue might be separate internal requests (not sub-requests that you could halt with [NS]).
See here.
simplest answer just add a canonical link in your head of your html document, this will stop your duplicate content issue.
Related
I have a multilingual wordpress website and want to redirect website of given region to given language,
xyz.de --> xyz.de/de/
xyz.co.uk --> xyz.co.uk/en/
direct access to xyz.de/de and xyz.co.uk/en are working properly. So there is no problem on wordpress side.
Now, I am trying to change the htaccess file of xyz.de and xyz.co.uk so that they redirect the website.
Considering xyz.co.uk
I want to add a RewriteCond such that whenever there is no /en trailing after xyz.co.uk it will automatically add /en.
For example xyz.co.uk/<trailing address> results in xyz.co.uk/en/<trailing address>
So far I have the following code, which somehow doesn't seem to work,
RewriteCond %{REQUEST_URI} !^/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
The negation of /en is not working! I have also tried
RewriteCond %{REQUEST_URI} !/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
Could someone tell me where I am going wrong? seems like I have gone wrong in writing RegEx and suggest if there is better way to achieve the same, that does not affect the SEO across different domains.
Use THE_REQUEST variable instead of REQUEST_URI:
RewriteCond %{HTTP_HOST} \.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !/en/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [L,R=302,NE]
Make sure to keep this rule as your very first rule in .htaccess.
Change it to R=301 once you've tested.
I have begun learning htaccess and i'm massively struggling. I'm simply trying to remove my .php from a URL
subdomain.example.com/weddings.php
to
subdomain.example.com/weddings/
to be honest that's not entirely it. the full url is
subdomain.example.com/weddings.php?section=pyromusicals#divname
which im trying to make into
subdomain.example.com/weddings/divname
for now though just the removal of the .php to / would be helpful.
so far I have in my htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
which is re-writing to
subdomain.example.co.uk/wedding-fireworks/
which is great but it also generates a 404 error page not found.
The requested URL /wedding-fireworks/index was not found on this server.
Is this what you are wanting to do?
RewriteRule ^([^/]+)/([^/]+) /$1.php?section=pyromusicals#$2 [QSA,L]
RewriteRule ^([^/]+) /$1.php [QSA,L]
Note that is assuming your section will always = pyromusicals
If section changes, you will need to include it in your URL also example below.
http://www.yoursite.com/somephppage/nameofsection/divname
Then your mod rewrite would look like this.
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /$1.php?section=$2#$3 [QSA,L]
RewriteRule ^([^/]+) /$1.php [QSA,L]
I am trying to configure a dynamic mod rewrite rule in my .htaccess file using mod_rewrite. I am very close to figuring this out. I am trying to get URLs like these:
http://www.mysite.com/index.php?service=14&title=events
http://www.mysite.com/index.php?service=48&title=planning
To automatically be rewritten to these:
http://www.mysite.com/service/14/events
http://www.mysite.com/service/48/planning
Here is my codes so far:
RewriteRule ^service/(.*)/(.*)$ index.php?service=$1&title=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^service=$1&title=$2 [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php [NC]
RewriteRule ^index.php$ /service/$1/$2 [L,R=301]
I think there is something wrong with the last line maybe? I'm not the best at regular expressions so any help would be greatly appreciated.
Edit: Just wanted to be clear that the pretty URLs do work. However, the old URLs aren't redirecting and are still displaying in the browser.
I am not sure if this would be any easier but if I could get the URLs to look like this:
http://www.mysite.com/service/14/title/events
http://www.mysite.com/service/48/title/planning
Then that would work too. I don't really need the second query title to be in the URL but if it's easier to leave it in there, then no big deal.
Edit: Answered
Many thanks to all who helped contribute to the solution. I got this for my rewrite rule:
RewriteRule ^index/service/(.*)/(.*)/$ index.php?service=$1&title=$2
Once I added 'index' it worked with both query strings. As far as the redirect goes, I edited my php script and did all the URL redirecting there, which was a lot easier. Special thanks to mkjasinski for pointing that out.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^service/([0-9]+?)?/([a-zA-Z\-]+?)$ index.php?service=$1&title=$2 [L,NC]
and if I have run: http://localhost/service/12/This-is-text in $_GET in index.php:
array (size=2)
'service' => string '12' (length=2)
'title' => string 'This-is-text' (length=12)
you can't use special holders ($1,$2...) from previous rewrite rules into rewritecond.
change your code to:
RewriteRule ^service/(.*)/(.*)$ index.php?service=$1&title=$2 [NC,L]
RewriteCond %{QUERY_STRING} (?:^|&)service=([0-9]+)(&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)title=([a-z]+)(&|$) [NC]
RewriteRule ^index.php$ /service/%1/%3? [L,R=301]
I'm diving into the uses of htaccess/mod rewrite for the first time and I'm having a little bit of trouble with a redirect/mask.
I have a test directory in my root folder called modrw, in that folder is a index.php file with a nice and simple:
<?php echo $_GET['name']; ?>
In the browser if I type www.domain.com/modrw/{word}/ then the word is echoed on the page, which is what I want.
If I type www.domain.com/modrw/name={word} then I am redirected to www.domain.com/modrw/{word}/ and the word is also echoed as I intended.
However, if I direct the browser to the URL www.domain.com/modrw/?name={word}/ the word is echoed but I am not redirected to www.domain.com/modrw/{word}/ like I hoped.
How is the ? causing troubles? In the RewriteRule code below the ? is included, I've tried it a couple different ways but can't get it working.
Here is my htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteRule ^modrw/?name=([^/\.]+)/?$ http://www.domain.com/modrw/$1 [R]
RewriteRule ^modrw/([^/\.]+)/?$ modrw/?name=$1
What is causing the problem, is there a specific way to include the ?, does it not pick this up at all? Am I taking completely the wrong approach?
I've also tried using Options +FollowSymlinks but I'm not entirely sure what this does nor if it's needed at all...
Remember that RewriteRuleonly matches REQUEST_URI. To match query string you must use RewriteCond with variable %{QUERY_STRING}. For ex. in your case:
RewriteCond %{QUERY_STRING} ^name=(.*)(&|$) [NC]
RewriteRule ^modrw /modrw/%1? [L,R,NC]
I have a tricky issue redirecting some URLs internally for my site.
The situation is this, I currently have a URL like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]
RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]
However I would also like to be able to redirect to check.php using a URL like example.com/youtube.com. Unfortunately it is just beyond me to figure it out.
I have a directory /assets/ with all the CSS, JS, etc. which shouldn't be affected.
Thanks
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]
This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.
As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?