I'd like to prevent Apache from urldecoding %20 from my URLs because I use parts of the URL as parameters in my Controllers.
My .htaccess used to look like this
Options -MultiViews
RewriteEngine ON
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
And that was basically perfect except for %20 being decoded when URLs like "/path/to/something%20interesting/" where accessed.
I read, that THE_REQUEST provides a non-decoded URL-string. Now I'm trying to use the path-part of THE_REQUEST for my rewrite. I wrote a regular expression that matches the part of THE_REQUEST I want, but I don't know, how to pass the string to the RewriteRule.
RewriteCond %{THE_REQUEST} ^(/([a-zA-Z0-9/\-_()]|(%20))+)$
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L,NE]
Any help is appreciated. Thanks.
Related
I'm trying to make a rewrite rule which will understand
http://example.com/test/1234
as
http://example.com/test.php?t=1234
This is what I have right now and it doesn't work:
RewriteEngine on
RewriteRule ^test?t=([^/\.]+)/?$ http://mywebsite.com/test/$1 [L]
Can someone give me a hand?
Your rewriteRule is backwards in that you're supposed to match on the left what you want the clean url to look like and rewrite on the right to where the file is located on the server. But even if it weren't backwards, you'd have to escape the question mark character in the RegExp. But since it is backwards, you should be using something closer to:
RewriteEngine on
RewriteRule ^test/(.*) test.php?t=$1 [L]
Try :
RewriteEngine On
RewriteCond %{THE_REQUEST} /test.php\?t=([^&\s]+) [NC]
RewriteRule ^test.php$ /test/%1? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([^/]+)/?$ /test.php?t=$1 [QSA,L,NC]
Query string is not part of match in rewrite rule directive, Use %{QUERY_STRING} or %{THE_REQUEST} variables to match against the query string.
I need to use htaccess to change below url:
http://example.com/main/en/index.php?page=pages&page_id=9
to:
http://example.com/main/en/pages/9.html
I've done it by the below rule:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /main/fa/?index.php?page=$1&page_id=$2 [L]
but there is a problem, this rule make my display url to:
http://example.com/pages/9.html
I want to htaccess change url just after last slash.
Because I must have more than one language, it will have conflict with other languages. How must I write this rule?
Maybe this is what you are looking for:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([^/]+)/([^/]+)/([\d]+)\.html/? [NC]
RewriteRule .* main/%1/index.php?page=%2&page_id=%3 [L]
Will redirect internally:
http://example.com/main/LangCode/PageName/PageID.html
To:
http://example.com/main/LangCode/index.php?page=PageName&page_id=PageID
For example I have this URL: http://www.domain.com/index.php?mod=category&continent=Asia
I wanted to truncate the URL by removing the index.php, so the output URL would be: http://www.domain.com/?mod=category&continent=Asia
Is there any alternative(preferably shorter) code than this one:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !(index.php) [NC]
RewriteRule ^(.*)$ blah-blah-blah [L]
I think that what you are saying is that you want your external URIs of the form
http://www.domain.com/?mod=category&continent=Asia
to be processed by DOCROOT/index.php
Specifying a DirectoryIndex of index.php will achieve this as will
RewriteRule ^$ index.php
The rewrite engine will automatically append the query string.
To truncate the site URIs in your web pages, you need to change the HTML.
i want to change a url like : localhost/site/home.php?p=index to localhost/site/index
i use this code in my htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS]
but when i write like localhost/site/home.php?p=profile.user i get the 404 error, and go to this link
localhost/profile.user
so how can i fix itthanks
Let's look at your rewrites first:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS]
This is relative rewrite: the replacement text home.php... does not begin with a slash. Relative rewrites in a per-directory context (<Directory> or .htaccess) require a RewriteBase directive to be configured, otherwise they do the wrong thing.
Secondly, your rule is backwards, If you want to rewrite the home.php URL to the site/index one, you have to put the home.php match on the left side, and the site/index on the right:
RewriteRule ^home.php?p=(.*) /site/$1
Notice that I have an absolute rewrite. This means that mod_rewrite will create a URL out of the rewrite by sticking http://example.com on it. A new request is internally generated now for http://example.com/site/<whatever>. We can get away without using RewriteBase since we have no relative rewrites.
As for your last question, it is not clear why when you access localhost/site/home.php?p=profile.user you're being taken to localhost/profile.user. I'm suspecting that it's your home.php script doing that, perhaps. You're trying to use mod_rewrite to hijack that particular kind of PHP request and send it elsewhere, right?
What you meant is probably: you want to rewrite this way:
http://mysite.com/index => http://mysite.com/home.php?p=index
So this should work
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?$ /home.php?p=$1 [QSA,L]
Now if you want the opposite:
http://mysite.com/home.php?p=index => http://mysite.com/index
This should work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /home\.php$ / [QSA,L]
What I have
newsy/czytaj/items/odbierz-250zl-na-reklame.html
This is what I would have
newsy/odbierz-250zl-na-reklame.html
How to do this with mod-rewrite? I don't understand RewriteRule.
My .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} (ftp|https?):|/etc/ [NC,OR]
RewriteCond %{QUERY_STRING} (ftp|https?):|/etc/ [NC]
RewriteRule .* - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*\.html$ index.php [L]
RewriteEngine on
RewriteRule ^newsy/([^\./]+)\.html /newsy/czytaj/items/$1.html [L]
This will rewrite anything that starts with newsy and add a /czytaj/items between it and the html file.
In principle you just create a corresponding rewrite rule:
RewriteRule ^newsy/czytaj/items/(.+) /newsy/$1 [L]
It is crucial not to omit [L]flag. Otherwise your rewrite engine may get stuck in an endless loop. Also in the beginning of the .htaccessfile remember to enable mod_rewrite with:
RewriteEngine On
For more help on mod_rewrite, I recommend checking out mod_rewrite-cheatsheet. For an exhaustive URl Rewriting Guide see a corresponding page from Apache 2.0 Documentation.