I have the standard htaccess which just catches all and puts it into one url parameter which is later processed in code...
RewriteRule ^config/ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I use "pretty" url params like
/some/route/param1/value1/param2/value2
but now I need an ability to add "regular" url params like
/some/route/param1/value1?param2=value2¶m3=value3 etc
I tried adding
RewriteRule ^(.*)\?(.*)$ index.php?q=$1&p=$2 [L,QSA]
before the existing rule, but it won't work properly (and I suspect it would only work with one parameter).
Try this:
RewriteRule ^([^?]+)\?(.*)$ index.php?q=$1&p=$2 [L,QSA]
Related
Yes, I checked the related questions posted in SO, but could not find something that would help me.
My .htaccess has plenty of redirects already, and they work fine, but this one is giving me the go around.
I had a URL like:
http://example.com/comp_all.php?vid_mod=529
which I changed to a more friendly one:
http://example.com/comparatif-voiture/Audi/A4/529
In order to accomplish that I added the following rule within .htaccess:
RewriteCond %{REQUEST_URI} ^/?comparatif-voiture [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9]+$) /comp_all.php?vid_mod=$1 [L]
And that works fine, as well.
Now, I want to have the 'old and ugly' URL still sitting out there to be redirect to the 'nice' ones.
I tried the following:
Redirect 301 /comp_all.php?vid_mod=529 http://example.com/comparatif-voiture/Audi/A4/529
But that does not work. It just shows the 'ugly' URL.
It does not matter whether I placed the above redirect before or after the Rewrite Rule.
You cannot use query string in Redirect directive. You need RewriteCond in mod_rewrite like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+comp_all\.php\?vid_mod=([0-9]+) [NC]
RewriteRule ^ /comparatif-voiture/Audi/A4/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^comparatif-voiture/.+?/([0-9]+)/?$ /comp_all.php?vid_mod=$1 [L,QSA]
PS: I have also simplified your other rule.
i have a special situation and i can't find a good solution. I've already seen dozens of questions/answers here but none of them seems to solve my problem!
I have an url like this:
https://subdomain.domain.com/{user-name}/{app-name}/
"user-name" and "app-name" can change everytime and i need to redirect it to
index.php?u={user-name}&a={app-name}
But if the url is only https://subdomain.domain.com/{user-name}/ i need to redirect it to
store-list.php?u={user-name} (to show a list of all available apps for that user).
My .htaccess is currently like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /store-list.php?u=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?u=$1&a=$2 [L]
It redirects perfectly on both situations but every call to a "real" file doesn't work (for example a call to a js or css file inside my html).
What am i doing wrong??
Making one RewriteCond Set Apply to Several Rules
Yes, we're really close... but a RewriteCond only applies to one rule. That's what is throwing us off. Let's use some tricky logic and put the conditions in reverse:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,S=2]
RewriteRule ^([^/]+)/?$ /store-list.php?u=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?u=$1&a=$2 [L]
The conditions say that if the files do exist, leave them unchanged, and to skip the two next rules (S=2).
I was wondering if there is a way to achieve this sort of URL rewriting;
RewriteRule ^example-link-1?$ /blog/example-link-1 [NC,L]
RewriteRule ^example-link-2?$ /blog/example-link-2 [NC,L]
RewriteRule ^example-link-3?$ /blog/example-link-3 [NC,L]
RewriteRule ^example-link-4?$ /blog/example-link-4 [NC,L]
where you go directly to a link such as example.com/example-link-1 and you are actually shown content from example.com/blog/example-link-1, but without the need for a new rule per page i add. Is there a dynamic way to do this in the htaccess? ie;
IF exists('blog/'+pageURI)
THEN rewrite to 'blog/'+pageURI but keep the uri without 'blog/' in it.
Yes sure you can do:
RewriteRule ^(example-link-[1234])/?$ /blog/$1 [NC,L]
for above 4 rules. But in general you can do this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/blog/$1 -f
RewriteRule ^(.+?)/?$ /blog/$1 [NC,L]
This will rewrite to /blog/something if /blog/something exists while keeping the same URI without /blog/.
I want a simple redirect in my .htaccess, with the goal of making a "shortlink" to a long URL.
mydomain.com/short
to take the user to
http://www.mydomain.com/blahblah/foo/bar/foobar/uglylongurl.html
So I tried this:
Redirect /short http://www.mydomain.com/blahblah/foo/bar/foobar/uglylongurl.html
but within the same .htaccess file is:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This is causing my simple redirect to have "short" appended as a query string.
I have tried [R] to redirect immediately and I've also tried [L] to stop processing if the first (simplest) rule is used. Both give me a 500 error.
I hope someone knows what I'm missing here. I am on a tight deadline and this is just killing me :P Thanks in advance for any help.
Many thanks to the responder who got this working. I had the redirect above the other rules, however, I needed to change it to a RewriteRule and add the additional code as in his example.
One more issue arose after this....and with his suggestion, I am adding the next layer of the problem to this question (instead of to the comment reply, where the code tags didn't work and it was hard to read).
So here is my next issue. The first one in the list works just fine, whether redirecting to an internal page or an external URL. But subsequent rules give me a 404 error. Here is what it looks like (and note they are all before the one that appends the query string):
RewriteRule ^short/?$ /ugly/long/url.html [L,NC]
RewriteRule ^/sweet/?$ /another/ugly/long/url.html [L,NC]
RewriteRule ^/offsite/?$ http://www.somewhereelse.com/with/a/long/url.html [L,NC]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Order of rewrite rules in pretty important. First have your desired rule then rest of the rules.
RewriteEngine On
RewriteRule ^short/?$ /blahblah/foo/bar/foobar/uglylongurl.html [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I'm struggling with creating a set of rules in my .htaccess file, resulting in constant 500 Internal Server Errors instead.
RewriteRule ^(.*)\.php$ /router.php?rq=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /router.php?rq=$1 [L,QSA]
I want my .htaccess to behave like this:
All requests (except resources, images, CSS, js, etc) should
redirect to /router.php, preserving query strings. I don't need the
REQUEST_URI as a query string as it is available via
$_SERVER['REQUEST_URI'] regardless.
All attempts to access a PHP file directly, for example user enters
"index.php", should also redirect to router.php.
You can use this rule for your routing:
RewriteCond %{REQUEST_URI} !/router\.php [NC]
RewriteRule \.php$ /router.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /router.php [NC,L]