I've seen examples in htaccess files using mod_rewrite where everything is done through one php file and different URLs are redirected back to index php.
RewriteRule ^registration(|/)$ /index.php
I'm curious as to what (|/)$ does/is. I've read a lot of stuff and can't seem to find any mention of the use of a vertical bar in mod_rewrite and if I remove this, the redirect still works fine.
The vertical bar stands for a logical OR, and lets you specify either a trailing slash after 'registration' or not.
I prefer using a '?' after the slash, making it optional:
RewriteRule ^registration/?$ /index.php
Related
I am trying to get some old URL's to redirect to the homepage. I tried it like below but it doesn't appear to work. Also tried some regex, but no results.
RewriteRule ^blog/%20article%20name(/?)$ / [R,L]
How is this to be done?
Converting my comment to answer so that solution is easy to find for future visitors.
To be able to match whitespace in URI, you can use perl property for whitespaces \s in your rule like this:
RewriteRule ^blog/\sarticle\sname/?$ / [R=302,NC,L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
I have a Wordpress installation on a LAMP stack, and if I have a post at http://example.com/abc/ , I would like URLs like http://example.com/abc/def.html to be redirected to http://example.com/abc/ . (Note that the slot here occupied by "def" should be without any slashes; this means among other things that things under http://example.com/wp-content/ should be unhindered.)
The rewrite I tried is:
RewriteRule ^(/[^/]+/)[^/]+\.html$ $1 [R=301,L]
As far as I can tell, that says, "Take the first two slashes and everything between them, matching on no more slashes and ending in .html, and redirect to the first captured group." However, with that in place, I can access http://example.com/abc/ , but I get a 404 on attempted access to http://example.com/abc/def.html .
What should I be doing to put the desired redirect behavior in place?
Thanks,
Try this rule:
RewriteRule ^/?([^/]+/)[^/.]+\.html$ /$1 [NC,R=301,L]
make leading slash optional as .htaccess doesn't have it and tweak part after first slash. Make sure this is your very first rule.
I would like to restructure some folders on my website, specifically I am want to move what's contained inside "images/" to "images/gallery/", but I don't want to break previous links, so I thought of using htaccess.
I looked up several tutorials and even several questions here on stackoverflow, tried several times, but I can't get the rewrite rule to work.
This is what I have:
RewriteRule ^images/(.*) /images/gallery/$1 [R=301,NC,L]
But when I try to access anything inside /images/ (for example images/test.jpg) it stays into images/test.jpg and doesn't go to images/gallery/test.jpg. So it doesn't seem to have an effect.
Any clue on what I might possibly doing wrong?
Thank you!
Your rule at present will cause a redirect loop since /images/ is present in both source and target URLs and you're not even using anchor $:
You can tweak your regex like this:
RewriteRule ^images/([^/]+)$ /images/gallery/$1 [R=301,NC,L]
Now pattern will match /images/test.jpg but won't match redirected URL /images/gallery/test.jpg due to use of [^/]+ in pattern.
Make sure this rule is first after RewriteEngine On and there is no .htaccess in /images/ folder.
EDIT: If your original path has sub-directories also then use:
RewriteRule ^images/((?!gallery/).+)$ /images/gallery/$1 [R=301,NC,L]
I know there are many questions about Apache RewriteRules, especially for removing trailing slashes. I have looked at tons but I can't seem to find anyone trying to solve this problem.
I am using Magento, so the URL structure looks like this:
example.com/index.php
example.com/index.php/
example.com/index.php/page1/
Here is my ideal URL structure:
example.com
example.com/page1/
example.com/page2/
So basically I just want to strip the index.php AND make sure the naked domain does not have a trailing slash (example.com instead of example.com/). Also, I would like to NOT include the hardcoded domain name if possible so that the rewrite can be applied in different environments.
Here is my current Rewrite...
RewriteCond %{REQUEST_URI} ^/index\.php/?
RewriteRule ^index.php/(.*) /$1 [R=301,L]
This seems to work in all situations, except for:
example.com/index.php (doesn't work at all)
example.com/index.php/ (leaves the trailing slash)
I would appreciate any regex advice! Thank you.
UPDATE
Thanks to the answer below from #zx81 I have successfully stripped all URLs down to the root domain, but still can't remove the slash.
So here is the current URL: example.com/
And I can't remove the trailing slash!
Not able to test it live, but try this.
RewriteRule ^index\.php()/?(?:([^/]+)/)? $1$2 [R=301,L]
It should handle the one that doesn't work at all thanks to the empty capturing group 1 ().
In PCRE (Apache's regex flavor) this also strips the trailing slash, but Apache may decide to add it back.
Due to some bad URLs, we generated some links that don't work and I want to redirect them with a 301 redirect to clear up some webmaster tools issues with Google.
So, we have this URL like this:
http://www.site.com/subdomain/z//-products
*Note that subdomain is variable, the rest of the url is static.
As a side note, this URL makes no sense, that's why I want to redirect it. It should be something like this:
http://www.site.com/bedroom/z/12345/bedroom-furniture-products
Anyway, we had these bad URLs being dynamically generated. We've fixed them, but google picked them up and keeps trying to crawl them. I want to create an htaccess rule to 301 redirect them and the issue should wash out eventually.
Here's what I tried with htaccess to no avail:
^(.*)/n//-products/?$ $1 [R=301,B]
I've also tried all kinds of permutations of this and it's not working. I suspected it was an entity escaping issue, but my research led me to add the [B], but that didn't seem to work either. It's like the redirect rule is working, but it's just redirecting to the original page.
What am i missing here?
I believe anubhava is correct, in that there is inconsistency between the sample URL you describe /subdomain/z//-products and the RewriteRule you attempted to apply. Not sure if this is a typo or not. It may even be the case your copy/paste operation actually added the "/n" literally.
Anyhoo, let us presume that you want to make the rule work with /subdomain/z//-products:
RewriteRule ^/([^/]+)/z//\-products/?$ http://www.site.com/$1 [R=301]
See the example 1 slides of this PDF to get the quick first portion. It is much faster than using (.*).
We literally match the z character and the surrounding slashes. We escape the - character, then we do the rest of the URL and optionally match the trailing slash. We use, if memory serves correctly, an "external" style redirect so that the robots re-open a separate HTTP connection, appending the matched backreference, and hand off the status code.
Let me know if that works.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^subdomain/z/-products/?$ /$1 [L,R=302,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.