.htaccess RewriteRule to remove ? following hostname - .htaccess

I have an htaccess file with some rules, and I now want to add another rule which strips a part of the URL such that
www.mydomain.com/?generations/anything
becomes
www.mydomain.com/anything
(and anything means any other characters). I can make it work without the ?, but I can't seem to match/remove the ?. I tried:
1 failed:
RewriteRule ^\?generations/(.*)$ /$1 [L]
2 failed:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^generations/(.*)$ $1 [L]
3 failed:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^([^?]*)?generations/(.*)$ $2%1 [L]
4 failed:
RewriteRule ^([^?]*)?generations/(.*)$ $1$2 [QSA,L]
Can someone create a working rule - and explain why my third attempt above is not working? I can potentially see problems with the first two, but the third and fourth should work...

You're getting there with 3, but the query string is not included in a RewriteRule match. So you just want to match the empty string:
RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^$ %1? [L]
But you also have it the wrong way round I think, if you want /anything to be the URL that is visited in the browser. You actually want just this:
RewriteRule ^(.*)$ ?generations/$1 [L]

Related

htaccess rewrite url with a parameter that equals 0

I would like to change
detail.php?cid=$category&ccid=$category2&cccid=$category3&fid=$id&ftitle=$titleseo
to
detail/$category/$category2/$category3/$id/$titleseo
But in case one of the parameters equals 0, the parameter should not be visible.
So in case $category=0, the url should become:
detail/$category2/$category3/$id/$titleseo
In case $category2=0, the url should become:
detail/$category/$category3/$id/$titleseo
Is this possible with a rewrite rule in htaccess and if yes how should this look like?
You could use this considering /$category/$category2/$category3/ have given names /$id/$titleseo are fixed :
RewriteEngine On
RewriteBase /
RewriteRule ^detail/(\$category)?/?(\$category2)?/?(\$category3)?/?(\$id)/(\$titleseo)$ - [E=PASS:cid=$1&ccid=$2&cccid=$3&fid=$4&ftitle=$5]
RewriteCond %{ENV:PASS} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule ^ details.php?%1%2%3%4 [L]
RewriteCond %{QUERY_STRING} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule ^ details.php?%1%2%3%4 [L]
RewriteCond %{ENV:PASS} ^(.+)$
RewriteRule ^ details.php?%1 [L]
In general use this :
RewriteEngine On
RewriteBase /
RewriteRule ^detail/([^/]+)?/?([^/]+)?/?([^/]+)?/?(\$id)/(\$titleseo)$ - [E=PASS:cid=$1&ccid=$2&cccid=$3&fid=$4&ftitle=$5]
RewriteCond %{ENV:PASS} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule ^ details.php?%1%2%3%4 [L]
RewriteCond %{QUERY_STRING} (.*)(&)[a-z]+=&(.*)$|[a-z]+=&(.*)$
RewriteRule ^ details.php?%1%2%3%4 [L]
RewriteCond %{ENV:PASS} ^(.+)$
RewriteRule ^ details.php?%1 [L]
The rules above , first make and environment by capturing any request start with details and ended with /$id/$titleseo then check that ENV values to see which one has/hasn't a value and finally redirect it accordingly.
Both rules work in case of three levels of categories if you need more you could edit the rules like this :
Add this ([^/]+)?/? after RewriteRule ^detail/ then [E=PASS:cid=$1&ccid=$2&cccid=$3&ccccid=$4&fid=$5&ftitle=$6] at the end , you could compare to see the difrenec between this and previous one to get a criteria , and finally add %5 to /details.php?%1%2%3%4 [L] like this /details.php?%1%2%3%4%5 [L] and so on .
Did not get the code from Mohammed to work so far, but used
RewriteRule ^detail/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) detail.php?cid=$1&ccid=$2&cccid=$3&fid=$4&&ftitle=$5

What's wrong with this RewriteRule?

Here's my code in htaccess:
RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC]
RewriteRule ^ /user/%1? [R=301,L]
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
What I want it to do is take the URL /user/?user=username and rewrite it to /user/username
What I have successfully rewrites it to /user/username but then it gives me a 500 error. If anyone could tell me why I would be very appreciative.
Thanks!
Edit:
The 500 error seems to be because it's creating a redirect loop with this rule. I'm not sure how to take just the last part and append it to the URL as a query string.
Please change this line :
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
with this :
RewriteRule ^user/[a-zA-Z]+$ /user/?user=$1 [L]
You should match only letters , otherwise the above condition RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC] doesn't make any sense with (.*)$

Im trying to combine three various clean url rules

my problem is that I want to get three simple rules working, but my knowledge is too little, to get them working together:
These are obvious:
RewriteRule ^login$ /login.php [L]
RewriteRule ^register$ /register.php [L]
domain.com/login and domain.com/register
Secondly, since i have only one page used for displaying data, i want its url to be as simple as posible, like:
RewriteRule ^(.*)$ /data.php?id=$1 [L]
which should be translated into:
domain.com/1a2s3d
As third, I want to be able to change url with activation code:
RewriteRule ^register/activate/([^/]+)$ /register.php?action=activate&code=$1 [L]
which finally should be translated into:
domain.com/register/activate/some-hash
I know just simply basics. And I cannot mix all of these three ideas into one single working htaccess file. With the second rule the server gives me 500 error, with third rule registration page works, but css file path is translated into domain.com/register/activate/theme/style.css instead of domain.com/theme/style.css
Any help would be appreciated.
Try just with that:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^login$ /login.php [L]
RewriteRule ^register$ /register.php [L]
RewriteRule ^register/activate/([^/]+)$ /register.php?action=activate&code=$1 [L]
RewriteRule ^(.*)$ /data.php?id=$1 [L]

Dont redirect if parameter is set

I want to redirect a url except when there is a parameter named login with the value false.
Tested it on several regexp testers and it works like I want.
But when it comes to the server it doesn't.
I always get redirected to ?login=true.
Why?
RewriteRule (.*)write((?!/?login=false).)*$ ?login=true [L]
RewriteRule only matches against everything after the hostname and before the query string. You'll need to use RewriteCond and the %{QUERY_STRING} variable.
RewriteCond %{QUERY_STRING} !login=false
RewriteRule (.*)write$ ?login=true [L]
See the documentation for more information.
RewriteRule (.*)write$ ?login=true [QSA]
RewriteCond %{QUERY_STRING} !name=
RewriteRule .? - [S=1]
RewriteRule (.*)write$ ?login=true [L]
made it like this now
works for me

htaccess two parameter mod-rewrite problems

I'm trying to setup a more friendly URL system and failing miserably. I want to be able to pass 1 or 2 GET parameters like this:
http://website.com/1234/123456
where 1234 is the first param and 123456 is the second param.
In my attempts Apache keeps viewing the /1234/ as a folder and a parameter. Here's what I've tried so far:
RewriteRule ^([^/]*)/([^/]*)$ index.php?id=$1&pa=$2 [L]
and
RewriteRule ^(.*)/(.*)$ index.php?id=$1&pa=$2
RewriteRule ^(.*)$ index.php?id=$1 [L]
You could do it like this:
RewriteEngine On
RewriteRule ^/?index\.php$ - [L]
RewriteRule ^/?(?:([^/]*)(?:/([^/]*)/?)?)?$ /index.php?id=$1&pa=$2 [L]
It's a little strict and messy, so you could just try:
RewriteEngine On
RewriteRule ^/?index\.php$ - [L]
RewriteRule ^/?(([^/]*)(/?(.*)))?$ /index.php?id=$2&pa=$4 [L]
That will take everything past the last so host.com/123/1233/56 will rewrite to host.com/index.php?id=123&pa=1233/56 while the first won't rewrite it, because of the 56

Resources