.htaccess qualifier issue - php

Does anyone know what the problem is with this rewrite rule?
RewriteRule ^example/?(.*)/?(.*)/?(.*)$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
Shouldn't it work by detecting ? as a conditional?
Edit OK, let me rephrase my question.
How do you get Apache to ignore $1 - $3 if it's empty, and just instead go to example.php?
For example, instead of 3 lines:
RewriteRule ^example/(.*)/(.*)/(.*)$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
RewriteRule ^example/(.*)/(.*)$ example.php?id=$1&ud=$2 [QSA,L]
RewriteRule ^example/(.*)$ example.php?id=$1 [QSA,L]
I need one line to solve all 3 or more.

Greediness considerations make your rule equivalent to the following:
RewriteRule ^example/?(.*)$ example.php?id=$1 [QSA,L]
You probably want this instead:
RewriteRule ^example/([^/]+)/([^/]+)/([^/]+)/?$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
Based on the recent edit you did to your question, I'd use multiple rules:
RewriteRule ^example/([^/]+)/([^/]+)/([^/]+)/?$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
RewriteRule ^example/([^/]+)/([^/]+)/?$ example.php?id=$1&ud=$2 [QSA,L]
RewriteRule ^example/([^/]+)/?$ example.php?id=$1 [QSA,L]

Shouldn't it work by detecting ? as a conditional?
Sure, but backreferences are still filled in order. If the first parentheses (.*) match the empty string, then $1 is empty; there is no way to make this capture get "skipped".

The 404 error could be caused by a missing RewriteEngine. To solve the problem with the parameters, use:
RewriteEngine on
RewriteRule ^example/?([^/]+)?/?([^/]+)?/?([^/]+)?/?$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]

Related

htaccess rewrite rules conflict

www.example.com/category/sub_category/Having problem with the rewririte rules in .htaccess file.
my current .htaccess file looks like this.
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [NC,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [NC,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [NC,L]
RewriteRule ^([0-9]+)$ gallery.php?id=$1 [NC,L]
I'm trying create urls like the following.
www.example.com/product_name/1
www.example.com/category/sub_category/21
www.example.com/category/sub_category/product_name/233
www.example.com/gallery/872
www.example.com/gallery/872 is redirecting to www.example.com/category/sub_category/872 instead of gallery.php?id=872
edit:corrected url from www.example.com/gallery/872 to www.example.com/category/sub_category/872.
Your issue is that the first rule matches, the last one can never get applied...
RewriteEngine on
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?id=$1 [NC,L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [NC,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [NC,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [NC,L]
Rule of thumb: first the specific exceptions, then the more general rules.
The NC flag does not make sense if you also specify both, lower and upper case letters in your regex patterns. It is either/or, not and.
(note: I also included the correction #anubhava posted in his answer)
Your last rule will need regex modification since you're matching /gallery/872 and your pattern is only matching 1 or more digits.
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?id=$1 [NC,L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [QSA,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [QSA,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [QSA,L]
Also you need to recorder your rules like I showed above.

HTACCESS URL Variable Rewrite

Looking to rewrite a URL and can't seem to get it working.
Example URL:
http://www.example.com/find/agency.php?agency=33524&name=happy-example
Ideal, rewritten URL:
http://www.example.com/find/agency/happy-example
One of the many attempted rewrites:
RewriteRule ^/([.]+)/([.]+)$ agency.php?agency=$1&name=$2
RewriteRule ^/([.]+)/([.]+)/$ agency.php?agency=$1&name=$2
Even if the removal of the "agency=" variable isn't possible, I still can't seem to make this URL write out the variables - am I approaching the rewrite backwards?
Try using a rewrite condition to catch the query string before doing the rewrite.
For example something like:
RewriteCond %{QUERY_STRING} ^agency=33524&name=([^&]+)
RewriteRule ^find/agency.php$ find/agency/%1? [R=301]
There are several things wrong with your rule. However to get a URL like this.
http://www.example.com/find/33524/happy-example
You would need to use a rewrite like below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^find/([^/]+)/([^/]+)/?$ /agency.php?agency=$1&name=$2 [L]
Use a lazy modifier for the symbol matching (currently ([.]+) will match everything )
RewriteRule ^/([.]+?)/([.]+?)$ agency.php?agency=$1&name=$2
RewriteRule ^/([.]+?)/([.]+?)/$ agency.php?agency=$1&name=$2
You can also combine these into one rule
RewriteRule ^/([.]+?)/([.]+?)[/]{0,1}$ agency.php?agency=$1&name=$2
This does not include whatever flags you would need depending on your particular case

htaccess rewrite parameter name to "nothing"

I used the follow rule to rewrite example.com/user-beavis to example.com/profil.php?user=beavis
RewriteRule ^([^\-]*)user-(.*)$ $1profil.php?user=$2 [QSA]
But I want it to be example.com/beavis so I tried:
RewriteRule ^([^\-]*)(.*)$ $1profil.php?user=$2 [QSA]
but get 500 Internal Server Error
Is it even possible with any rule?
If you want to convert example.com/beavis to example.com/profil.php?user=beavis, use this:
RewriteRule ^(?!profil.php)(.*)$ /profil.php?user=$1 [L] [L]
I used a negative lookahead to protect from eternal loop.
This is the solution:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profil.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profil.php?user=$1

How to rewrite somedomain.com/free/single/result_id to somedomain.com/f/single/result_id

I'm having trouble getting my head around rewrite.
I'm trying to make it so that the URL somedomain.com/f/single/1 would grab the result from somedomain.com/free/single/1
I've tried the following.
RewriteRule ^/f$ /free$1 [L]
Here is a copy of my existing .htaccess with a suggestion made by Kamil...
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^f/(.+)$ /free/$1 [L]
I wonder if the problem is that I am already removing index.php before the /free ?
Can anyone help me understand what I'm doing wrong?
You look for following rule:
RewriteRule ^f/(.+)$ /free/$1 [L]
The first part ^f/(.+)$ is regular expression - it matches all strings starting with /f/ and stores the rest to the $1... then it's rewritten to /free/ and the rest stored in $1.

RewriteRule 500 Error Question

I have the following Rewrite Rules set up:
RewriteEngine On
RewriteRule ^api/([A-Za-z0-9-]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php/other/$1 [NC,L]
Unfortunately these cause my server to throw a 500 error. Taken individually, they both work fine though.
My intention is that if the request is http://somesite.com/api/whatever/, the first rule will get triggered, redirecting to index.php/api/whatever/
If anything other than "api" gets sent as the second segment though, it will redirect to index.php/other/whatever.
Is my understanding flawed somehow? I thought that it would go down the list, and with the L flag, would stop executing once it hit something. Or is my syntax wrong?
Cheers
whenever you get a 500, check /var/log/httpd/error_log (or the equivalent path on your system.)
I'm pretty sure the hyphen char in your character group is a regex syntax error. (also, the [NC] flag makes [A-Za-z] redundant
Try:
RewriteRule ^api/([-A-Z0-9]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([-A-Z0-9]+)$ index.php/other/$1 [NC,L]
Or perhaps
RewriteRule ^api/([^/]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([^/]+)$ index.php/other/$1 [NC,L]
I think you need QSA flag, try like that :
RewriteRule ^api/(.*)$ index.php/api/$1 [QSA,NC,L]
RewriteRule ^(.*)$ index.php/other/$1 [QSA,NC,L]

Categories