URL Rewrite always taking the first condition - url-rewriting

RewriteRule ^list lists.php [NC]
RewriteRule ^list/([^-]+)/ lists.php?listid=$1 [NC]
The problem is even if the link is like example.com/list/5 it is directed to list.php without the value. The second condition is not getting satisfied ever. How to solve this?

Try
RewriteRule ^list$ lists.php [L]
RewriteRule ^list/([\d]+)/$ lists.php?listid=$1 [L]
Notice that it will match list/5/ but not list/5. That would be:
RewriteRule ^list/([\d]+)$ lists.php?listid=$1 [L]
Edit: You can use [NC] if you want... I personally like the [L] flag and the case wording respected.

Related

Basic rewrite of url

I need a rule that rewrites
http://example.com/page.html?page=2
to
http://example.com/page.html/page/2
I already tried this rule:
RewriteRule ^(.*)page=(.*)$ http://example.com/$1/page/$2/
but this does not work! :( Where I'm wrong?
You can try this one:
RewriteCond %{QUERY_STRING} page=(\d+) [NC]
RewriteRule (.*) /$1/page/%1/? [R=301,L]
It should works.

Simple modrewrite rule with regex

This are my urls
http://test.aDomain.com/x-61bff
http://test.aDomain.com/x-ssmJhs54as65d4
http://test.aDomain.com/x-115545454
my rewrite rules
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
RewriteRule ^create-account create-account.php [L]
RewriteRule ^account account.php [L]
RewriteRule ^impress impress.php [L]
RewriteRule ^(x\-[a-zA-Z0-9]+) redirect.php?code=$1 [L]
The result is
http://test.aDomain.com/redirect.php
But why? it should be
http://test.aDomain.com/redirect.php?code=x-61bff
i dont get it ... can anybody help?
mainly because you got the regex wrong:
the / between the host-name and the path is really part of the path (e.g. it is /x-61bff instead of x-61bff). however, your regex matches only an x at the very beginning, thus /x-61bff will never match.
the minus-sign has only a special meaning within square brackets (e.g. [A-Z]); outside it is just another character, there is no need to escape it
so your rewrite-rule really should look like:
RewriteRule ^/(x-[a-zA-Z0-9]*) redirect.php?code=$1 [L]

Mod Rewrite won't redirect subdomains to new URL format

I'm changing all my subdomains to a single domains.
However, in order no to lose all my SEO I need to do some 301 redirections. My problem is that I have about 10.000 subdomains (it's a website about cities and each city is a subdomain) so I need to make a generic rewrite rule in order to make the new URLs (otherwise my htaccess will be too big).
I tried doing it myself but for some reason, it's doing what it wants to (so I guess I'm doing something wrong). Here is my code:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/^(.*)
RewriteRule ^(.*) http://domain.com/city/$1/b/$2 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*) http://domain.com/?multi_city=$1 [R=301,L]
This is what happens with these two rules.
city.domain.com --> domain.com/?multi_city=/
city.domain.com/b/place --> domain.com/?multi_city=/b/place
What am I doing wrong?
Thanks in advance.
So, after many hours, I finally fixed it doing this:
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.(.*)
RewriteCond %{REQUEST_URI} ^/b
RewriteRule ^(.*)$ http://mydomain.%2/city/%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.(.*)
RewriteCond %{REQUEST_URI} ^/event
RewriteRule ^(.*)$ http://mydomain.%2/city/%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com
RewriteRule ^(.*)$ http://mydomain.com/?multi_city=%1 [R=301,L]
This way I can redirect places and events first and if the URL is not in that format then it will go to the different format URL. It's probably not the most efficient solution but it works for me. Hope this helps to someone else.
I think the first RewriteCond it's wrong:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/^(.*)
The '^' symbol says that the string start, it's not part of the group, so I think that you will try:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/(.*)
Maybe, it will be better:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/b/([a-zA-Z0-9\-]+)
I'm doing it without testing, if it doesn't work I will make tests later and I'll answer you.

apache mod rewrite with the_request

I want to do the following with apache (mod rewrite).
if the user requests http://hostname.tld/index.php/folder/subfolder i want it to redirect (with a R=301) to http://hostname.tld/folder/subfolder.
if the user requests http://hostname.tld/folder/subfolder the request should internally be rewritten to index.php/folder/subfolder.
To prevent an endless redirect the first rule should check for %{THE_REQUEST}. The problem here is that I am unable to append "folder/subfolder" with a regex. How should I do this?
For the second rule I have this (and seems to work).
RewriteCond %{HTTP_HOST} hostname.tld [NC]
RewriteRule ^(.*)$ index.php [QSA,L]
The first one is still a problem.
I think the first one should be something like
RewriteCond %{THE_REQUEST} (.*)index.php(.*) [NC]
RewriteRule /index.php/$ http://hostname.tld/$1 [R=301,QSA,L]
But that is not really it.
The first should be.
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^index.php/(.*)$ http://hostname.tld/$1 [R=301,L]
I also see that your second rule redirects http://hostname.tld/folder/subfolder to http://hostname.tld/index.php (not http://hostname.tld/index.php/folder/subfolder). But as long as that works it's fine, as this it also prevents the redirect loop.
But just in case, here is the solution to add the folder/subfolder part:
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

mod_rewrite- matching everything after domain

This rule works fine for http://foo.com/page/contact
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
But what I want to do is for http://foo.com/contact
But this is not correct:
RewriteRule ^/([^/\.]+)/?$ index.php?page=$1 [L]
How do I correct that?
Remove your first / that's not needed because the root directory is already there. like so:
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
your second example is technically asking for http://foo.com//contact

Resources