htaccess URL rewrite: What am I messing up in the code? - regex

I have the following rewrite rule:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} Catalog\/(string|page)\/
RewriteRule ^Catalog\/(string|page)\/([^\/]+)\/[^\.]+\.html$ Catalog/Catalog.php?$1=$2
RewriteRule is on one line but may be showing on multiple lines here.
My questions is mainly what am I doing wrong. I am not getting any errors so Rewrite is working. The address I am typing in to the browser is www.domain.com/Catalog/string/RT/Round_Tomato.html and what I was hoping to get is www.domain.com/Catalog/Catalog.php?string=RT
I am guessing my regex is messed up but have not been able to get it right.

I think that the first wrong thing is putting point without backslash before (.) => www.
^www\.([^/])/Catalog/([a-zA-Z0-9]+)/([a-zA-Z0-9_]+).html$ www.$1/Catalog/Catalog.php?string=$2
here is a clear example:
RewriteRule ^http://www.remotesite.com/(.*)$
/mirror/of/remotesite/$1
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
check this out:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^Catalog/(.+)/(.+)\.html$ /Catalog/Catalog.php?$1=$2
you should restart your apache server -if it's a local server- (start and stop) and you must be able to access http://localhost/Catalog/string/RT.html

Related

Regex: "Mod Rewrite" everything from a "wildcard" to an address, without changing the address?

I really didn't know how to write the title. I changed it several times before I posted. But feel free to change it to the most appropriate question.
I also can't believe I couldn't find an answer already to this pretty basic thing I wanna to. I searched both here and on Google but couldn't find anything that answered this.
So I have this default WordPress .htaccess code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But what I would like to add, is the possibility of having all paths beginning with /cv/ to show the page for /cv/. So like a wildcard after, like /cv/*.
I tried with several versions of this:
RewriteRule /cv/.+ /cv/ - [L]
But none worked. Most things I tried redirected me to the "Couldn't find the page" page. But some just redirected back to /cv/. But I want the whatevers'-after-/cv/ should stay there. So if the address is for example /cv/hello, it should still be /cv/hello in the address but the page showing should be /cv/.
Don't think it should be so difficult. What have I missed?
ok, I set up a test now and got the following commands to work for domain.com/cv/hello
to redirect to domain.com/cv but keep the URL
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} \/cv\/(.+)$ [NC]
# make sure to exit here, if there already was a redirect (to prevent endless redirecting)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^.*$ /cv/%1 [NC,P,R=301,L]
The "magic" is to FollowSymlinks, to use P that tells apache to proxy-pass the redirect, so that the URL remains the same, and to check if there already has been a redirect to the current URL in order to avoid endless redirecting
I solved it temporary (not a nice solution but..) by adding a rewrite rule in my functions file. So that everything from cv/* points to a specific page. In this case page with ID 8472.
/**
* Add Rewrite Rule
*/
function custom_rewrite_basic()
{
add_rewrite_rule('^cv/(.+)/?', 'index.php?page_id=8472', 'top');
remove_action('generate_after_header', 'generate_featured_page_header');
}
add_action('init', 'custom_rewrite_basic');
So, this is just the solution for WordPress. But I don't know. Maybe the other answers on this page would have worked if it wasn't WordPress.

mod_rewrite all /something to index.html

I'm trying to rewrite all urls of the following pattern:
http://example.com/csfdg/anything
into http://example.com/csfdg/index.html
my .htaccess file at the root level contains this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule \/csfdg\/.* /csfdg/index.html [L]
The checker at http://htaccess.madewithlove.be/ tells me that it would rewrite my URLs the way I want, but when I go to http://example.com/csfdg/anything I just get a 404. It's very hard to tell what's going on, but I know that the RewriteEngine is working because if I mess with it enough I can get 500 errors to happen :)
Any thoughts? Thanks
You can use this :
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.html$
RewriteRule ^([^/]+)/.+$ /$1/index.html [L]
RewriteConditon is important here to avoid rewriting /foo/index.html to itself.

Regular expression endless loop

I have this server where two domain-names are pointed to. So in my .htaccess-file I want to make a simple rule that says something along the line:
If you come from test.domain.com then go to folder X, if you come from the-other-sub.domain.com then go to folder Y. And this means that I'm moving one of the subdomains, so I could like to make it so it redirects to the right URL (in case that people are following a deep link. For instance if people go to http://test.domain.com/path/to/a/page that they will be redirected to /path/to/a/page in the new folder.
I'm struggling to do so, though. What I don't get is, why is that this code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(.*)$ /test/$1 [L,R=301]
Leaves me, so if I go to subdomain.domain.com/abc , then the browser will send me to subdomain.domain.com/test/test/test/test/test/test/test/test/test/test/test/abc
and then complaining about that I have and endless loop. And please no smart links to Apache's documentation for mod_rewrite.c... I've read it, and this is where it has taken me. I know that the * means 'match 0 or more times', but I don't get why that copies the destination-string over and over and over...? /test/ isn't a variable in the regular expression, is it? So why does it repeat it?
If you redirect unconditionally to /test/... then it will keep adding /test/ before redirected URLs also.
To fix use this rule:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^((?!test/).*)$ /test/$1 [L,R=301,NC]
(?!test/) is negative lookahead condition which means add /test/ only if it already doesn't start with /test/.
The #anubhava's answer is a good solution.
In your current version (and in the #anubhava's one), the [L,R=301] flag causes the redirection to apply so the newly generated url is submitted again. It's why you must take care of not applying this redirection anew.
Nevertheless there is a simpler method, useful if you don't really need to generate a HTTP 301 response status code.
Then from your original version you can simply:
drop the initial "/" in the replacement expression
drop the "R=301" flag
So your version becomes:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(.*)$ test/$1 [L]
This way, the redirect will happen only once: at the second time, Apache will recognize that the url didn't change, and then stops looping.

Htaccess redirect urls that match with Regular expression

I'm trying to redirect any url's that match the following criteria:
old-domain.com/marketplace/businesses/anything-here/ads/
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/
etc...
to
new-domain.com/deal/
This is what my .htaccess file looks like
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^marketplace/businesses/([a-z0-9\-_]+)/ads/(.*) http://new-domain.com/deal/ [R=301,L]
</IfModule>
I've tried several variations os this including swapping the ([a-z0-9\-_]+) with ([A-Za-z0-9_-\s\.]+) and [a-zA-Z-_0-9]+) and other combos but I can't seem to get it to work.
Also notice where I said anything-here in the urls that means I want to be able to match pretty much anything that would be there.
Any help would be appreciated.
Thanks
The problem was that I had a physical directory at /marketplace/businesses/ with a .htaccess file that was preventing this from working.
I was able to add this to that .htaccess file and it worked like a charm.
Thanks to everyone who had a look and took a stab.
I think this might work. (Haven't tries it though)
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^marketplace/businesses/(.*) http://new-domain.com/deal/$1 [NC]
This will translate this
old-domain.com/marketplace/businesses/anything-here/ads/
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/
to this
http://new-domain.com/deal/anything-here/ads/
http://new-domain.com/deal/anything-here/ads/anyhting-after-this-too
http://new-domain.com/deal/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/
Or like this i you want to keep the parts intact, try this
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^marketplace/businesses/([a-zA-Z0-9\-_]+)/ads/([a-zA-Z0-9\-_]+) http://new-domain.com/deal/$1/$2 [NC]
This will be translated to this
http://new-domain.com/deal/anything-here/anyhting-after-this-too
but I don't think the other two will match the regexp.

Temporary redirect 302 with .htaccess and mod-rewrite matching expression

I'm trying to match a a bunch of redirects for my website with basically moved to a different folder on the server. I need to make http://www.site.com/index.php?page=anypage go to http://www.site.com/newfolder/index.php?page=anypage. The thing is http://www.site.com/index.php and http://www.site.com/index.php?page=home should remain untouched. How can I accomplish this?
I was trying the following in the .htaccess file, but I am affraid to make a mistake. I really don't know how to test this, either.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/index.php?page=(.*)$ http://www.site.com/newfolder/index.php?page=$1 [R=302,NC]
RewriteRule ^/index.php?page=home http://www.site.com/index.php?page=home [R=302,NC,L]
Now I figured that this is temporary, so I should know ho to reverse it! The next week, the links will have to redirect again to the root server. Also, what should I do to re-establish the normal redirection??
If I've followed your scenario correctly, you want something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !page=home
RewriteRule ^index.php /newfolder/index.php [R,L]
As far as testing goes, I prefer to try rules out on a local test server. If you have full control over the server (as is the case locally), there are some mod_rewrite directives that help you log what's going on, and that can be helpful in debugging. The module documentation has more information about this.
Edit: When you want to switch back, modify the RewriteRule above like so:
RewriteRule ^newfolder/index\.php /index.php [R,L]