We have several URLS and want to re-direct based on one of the segments.
Usually we've have the rewrite rule then something like:
redirect permanent /home/category/feline /home/category/cat
The challenge is there's something like 50+ urls and the segment changes, so I wanted to know if there is a simpler way to match and redirect?
Here's a few examples:
redirect permanent /home/category/feline /home/category/cat
redirect permanent /content/category/feline /home/category/cat
redirect permanent /channel/segment/category/feline /channel/segment/category/cat
redirect permanent /segment/category/feline/entry /segment/category/cat/entry
Is is possible to redirect any URLs with feline to cat?
I'm currently testing the following, which works, but wanted to clarify:
RewriteRule (.*)feline(.*)$ $1cat$2 [R=301,NE,L]
You can use RedirectMatch with regex support:
RedirectMatch 301 ^(.*/)?feline(/.*)?$ $1cat$2
Clear your browser cache before testing this rule.
Using mod_rewrite:
RewriteEngine On
RewriteRule ^(.*/)?feline(/.*)?$ /$1cat$2 [R=301,NE,NC,L]
Related
I want to redirect https://xxx.xxxx.com/zzz to https://xxx.xxxx.com/zzz247. I have tried this
Redirect 301 / https://xxx.xxxx.com/zzz247
So now when I try https://xxx.xxxx.com/zzz its take me into https://xxx.xxxx.com/zzz247zzz247zzz247zzz247zzz247zzz247zzz247....
How can I fix this issue? help please
What is required
I need enable only one URL which is https://xxx.xxxx.com/zzz247 even user type https://xxx.xxxx.com/zzz
Instead of Redirect directive use RedirectMatch with regex support:
RedirectMatch 301 ^/zzz/?$ /zzz247
Redirect appends current URI into redirected URI and since you're matching / that will match anything and cause a redirect loop.
Make sure to clear your browser cache before testing.
Using RewriteRule you can do this:
RewriteRule ^zzz/?$ /zzz247? [L,NC,R=301]
? at the end of the target URI is for stripping any query string.
I'm trying to redirect all existing urls to a new slightly simplified url structure, and wish to keep the existing links that have been indexed on google/bing intact.
The current url for posts looks like this /blog/post/some-random-post-here the
new post url's are virtually the same but no longer have /post/ in them. Instead its /blog/some-random-post-here
I have created a new htaccess rule for the new urls. So as of now both versions of the url's work. However I wish to keep one copy for SEO reasons. This is on a blog made from scratch (this is not WordPress or other blogging softwares)
Here are the rewrite rules in my htaccess for old and new urls.
**OLD URL:** RewriteRule ^post/([^/]+)/$ index.php?controller=post&action=view&post=$1 [L]
------------------------------------------------------------------------
**NEW URL:** RewriteRule ^([^/]+)/$ index.php?controller=post&action=view&post=$1 [L]
What would I need to add to the OLD URL structure in order to make a 301 permanent redirect to the new url structure? Both the OLD and the NEW rewrite rules are still active. But I rather 301 the old rule to the new URL.
Thank you in advance.
You can use RedirectMatch directive for this url redirection, it's easy.
Try :
RedirectMatch 301 ^/blog/post/(.*)$ http://domain.com/blog/$1
Or Try mod_rewrite To remove query strings
RewriteEngine on
RewriteRule ^blog/post/(.*)$ http://example.com/blog/$1? [NC,R,L]
Empty question mark ? at the end is important as it will discard the original query string from url.
will 301 redirect
http://example.com/blog/post/foo
to
http://example.com/blog/foo
I am familiar with redirecting URLs that don't have file types at the end, but not with ones that have .php at the end.
The website I'm working on has hundreds of indexed pages on Google that have this at the beginning of the url: http://example.com/index.php? with more information trailing afterwards.
An example is: http://example.com/index.php?main_page=index&cPath=1_18
The new index of the website is http://example.com/index or http://example.com
If I put http://example.com/index.php as the URL to be redirected, will it also redirect all index.php? Or do I need to put http://example.com/index.php* as the URL to be redirected or http://example.com/index.php?*?
Thank you!
If the main script to be executed is index.php, then there is no point in redirecting a url to itself...
Aabout those GET parameters in the url, if index.php oesn't use them, then.... they will be useless, but... thats it XD
The point is: does everything work without redirecting? If so, why redirecting at all?
If there are 301 redirects, ... that will mean more request to the server, and for those clients with low bandwith it does make a difference.
BTW, take a look at: htaccessredirect
Try:
RedirectMatch 301 ^/index\.php$ /
(or)
RedirectMatch 301 ^/index\.php$ /index
The query string at the end will automatically get appended.
If you're using wordpress now, you're not going to be able to use the mod_alias directive like above. You'll need to use mod_rewrite and place these rules above the wordpress rules:
RewriteCond %{THE_REQUEST} \ /+index\.php
RewriteRule ^ / [L,R=301]
(or replace the / at the end with /index)
On my site, users can add various URL's that need to be redirected.
For example; from this: domain.com/oldpage/36/
To this: domain.com/newpage/47/
They are added to the .htaccess like this:
Redirect 301 /oldpage/36/ /new-page/47/
But when accessing the old page they get this:
domain.com/newpage/47/?pid=36&pagename=oldpage
I'm pretty sure these rewrite rules are causing this predicament:
RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3
However, mod_rewrite stuff is not my strongpoint, so I have no idea how to fix it.
Any ideas ?
Adding a ? makes the Rewrite not add the query string to the url.
so this should work:
Redirect 301 /oldpage/36/ /new-page/47/?
As a precaution you could also add it to the end of:
RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1?
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3?
But only if they are needed
Since you are already using mod_rewrite anyway, I suppose you should make your redirects using rewrites too
RewriteRule /oldpage/36/ /new-page/47/ [R=301]
This will "rewrite" the URL from old to new, and will redirect the browser to new url with status code 301. [R] directive means redirect, which also stops other rules from processing, hence the other rules will be handled only when the new request is sent from broswer with new url.
I have a basic CMS in PHP/MySQL where content managers can create pages to the system for public viewing. Each page is then available at an url such as http://www.example.com/pages.php?pid=123
Now, I want to redirect requests to http://www.example.com/pages.php?pid=123 to http://www.example.com/pages.php?pid=456.
I've already removed the pid=123 page from the db but because of the cms code the site still returns a 202 when some one tries to access the page. I thought I could use a 301 redirect in .htaccess to make the redirect work, i.e.:
redirect 301 pages.php?pid=123 http://www.example.com/pages.php?pid=456
but this doesn't work, Apache still return 202 when trying to fetch the pid=123 page. Also, I've tried using mod_rewrite, but it doesn't work:
RewriteRule ^pages.php?pid=123$ pages.php?pid=456 [R=301,L]
Any ideas what could be wrong and how I can fix the 301 redirect?
Both the Redirect and RewriteRule directive work just on the URL path. In mod_alias (Redirect directive) you can not test the query and in mod_rewrite (RewriteRule directive) you need an additional RewriteCond directive:
RewriteCond %{QUERY_STRING} (^|&)pid=123(&|$)
RewriteRule ^pages\.php$ /pages.php?pid=456 [R=301,L]
But it would certainly be better if your CMS can handle such redirects since it’s your CMS that knows best what URLs are valid and what are not.
You can perform the redirect in PHP (which probably knows more about what to redirect where) using header().
Please note that ? is a special character used by regular expressions, so your regex matches pages.phppid=123 and pages.phppid=123.
Even then, I don't think the query string (including the ?pid=123 part) is used in the URL handled by RewriteRule, so you would need to use something like:
RewriteCond %{QUERY_STRING} ^pid=123$
RewriteRule ^pages.php$ pages.php?pid=456 [R=301,L]
This shouldn't work as is, but it should give you some ideas.