I have written this little piece of code. I am very new to this so i am not sure it is all correct. but basically it lets me access urls with the php extension. When people get on the site they are being redirected from the geo ip page to the correct language which like looks like this
main.php?lang=uk or nl or en or eu etc.
right now i can also use it like this
main/?lang=uk or nl or en or eu etc.
I would like to be able to to also remove the variable in the url ?lang=uk.
How would i do this. My .htaccess code is below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
DirectorySlash On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
</IfModule>
Thanks too anyone willing to help.
The first argument of RewriteRule does match anything after the domain name and prefix* and before any query string if that exists. In http://localhost/this/is/my/test?lang=123 with a .htaccess file in the this/is/ directory it would match my/test. To match a query string, you have to use the %{QUERY_STRING} variable.
If the second argument (the rewritten url) of RewriteRule does not contain a query string, it will automatically append the query string of the original url to the new url.
In the code below I use %{THE_REQUEST}. This is the string that is used to make the request for a page. It is in the form of GET /my/resource?query=string HTTP/1.1. It does not change when rewriting things, which makes it useful to prevent infinite loops.
In your php file make sure that the language is read from a cookie instead of a get variable, then do the following:
#Set cookie for language
RewriteCond %{QUERY_STRING} ^(.*?)&?lang=([^&]+)&?(.*?)$
RewriteRule ^ %{REQUEST_URI}?%1&%3 [CO=lang:%2:127.0.0.1:1:/:0:1,R,L]
#Remove potential prefix or suffix & from query string
RewriteCond %{QUERY_STRING} ^&(.*?)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*?)&$
RewriteRule ^ %{REQUEST_URI}?%1 [R,L]
#External requests with \.php should be without that.
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#Try to load php page if resource does not alreay have an extension
RewriteCond %{REQUEST_URI} !\.[a-z]+$
RewriteRule ^(.*)$ $1.php [L]
Related
I have a url like this:
http://www.localhost.com/code_category/computers/
I want to change this url to:
http://www.localhost.com/category/computers/
I don't need url redirection.
My current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You only want to redirect code_category to categoryexternally and keep the path as it is internally so, try this :
RewriteCond %{THE_REQUEST} !\s/+category/ [NC]
RewriteRule ^code_category/(.*)$ category/$1 [R=302,L,NE]
RewriteRule ^category/(.*)$ code_category/$1 [L]
The above will redirect any request containscode_category/whatever to category/whatever externally and keep the internal path as it is .
If you want only request contains code_category/computers/ change it to this :
RewriteCond %{THE_REQUEST} !\s/+category/computers/ [NC]
RewriteRule ^code_category/computers/(.*)$ category/computers/$1 [R=302,L,NE]
RewriteRule ^category/computers/(.*)$ code_category/computers/$1 [L]
test it , if it is fine change 302 to 301 for permanent redirection.
Note: clear your browser cache then test it.
.htaccess file
Add this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost.com [NC,OR]
# without redirect
# RewriteRule ^/code_category/computers/$ category/computers/
RewriteRule ^/category/computers/$ code_category/computers/
# redirect method
# RedirectMatch 301 ^/code_category/computers/$ category/computers/
RewriteEngine On enables mod_rewrite.
RewriteCond %{HTTP_HOST} shows which URLs we do and don't want to run through the rewrite.
In this case, we want to match example.com.
! means "not." We don't want to rewrite a URL that already includes folder1, because then it would keep getting folder1 added, and it would become an infinitely long URL.
[NC] matches both upper- and lower-case versions of the URL.
RewriteRule defines a particular rule.
The first string of characters after RewriteRule defines what the original URL looks like. There's a more detailed explanation of the special characters at the end of this article.
The second string after RewriteRule defines the new URL. This is in relation to the document root (html) directory. / means the html directory itself, and subfolders can also be specified.
For Reference click here
Hope this helps!
I have a site which uses a custom query string to deliver pages to the visitor. A sample query string looks like /?sec=news&pg=current&bg=203. I'd like to use .htaccess to rewrite the strings into a pattern like /news/current/203.
I know how to take the /news/current/203 URL and make it into a query string that I can parse with PHP. I typically use this bit of htaccess code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.*) /index.php?id=$1 [L]```
And my PHP then explodes id on the / and off we are running.
My question is this... How can I add a few more lines to this htaccess code that will allow the old URL query pattern to continue to work for people that have bookmarks or links that use the /?sec=news... pattern? Basically, it seems like I need to take this old query string and combine the values into one string that I can pass to index.php in the id parameter. I don't want to lose the ability to honor the old pattern, but also need to promote the new cleaner path-based string.
I know there is some regex that can help here, but I am terrible at understanding regular expressions. Any help would be appreciated, and let me know if I am not making any sense.
Update: The answer provided makes my final htaccess file look like this...
RewriteEngine on
# Prevent directory listings
Options All -Indexes
# Rewrite to www
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^sample.com[nc]
RewriteRule ^(.*)$ http://www.sample.com/$1 [r=301,nc]
# WHEN DONE TESTING< CHANGE ALL 302 to 301 !!!
# Honor the old format /index.php?sec=XXX&pg=XXX&bg=XXX and turn it into the new format
RewriteCond %{QUERY_STRING} sec=(\w+)&pg=(\w+)&bg=(\d+)
RewriteRule ^(.*)$ /%1/%2/%3? [R=302,L]
# Turn /path/to/page into index.php?id=path/to/page
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.*) /index.php?id=$1 [L]
Add this rule
RewriteCond %{QUERY_STRING} sec=(\w+)&pg=(\w+)&bg=(\d+)
RewriteRule ^(.*)$ /%1/%2/%3? [R=302,L]
Change 302 to 301
I'm making up myself a small blog and I found a useful .htaccess file to remove file extensions:
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php
This works just fine and all pages are showing up .php less. I know wanted to extend this so when I click a link to a specific blog post (say /blog/index.php?art=1) it just shows in the url as website/blog/1. I thought to tag on to the end of the .htaccess file:
RewriteRule ^blog/(.*)$ blog/index.php?art=$0 [L]
But that doesn't seem to be working. EDIT Actually it breaks the blog page so no snippets are pulled through from the DB
My .htaccess file is in the root directory and the blog files are /root/blog/index.php
Any help would be gratefully appreciated
Unlike most other languages, the parameters in .htaccess are not 0-based. To access the first parameter, you should use $1, not $0.
The following should work:
RewriteRule ^blog/(.*)$ blog/index.php?art=$1 [L]
It might also be worthwhile to add some tests in there, for example you might only want numerical values passed to art, so you can improve it using:
RewriteRule ^blog/([0-9]+)$ blog/index.php?art=$1 [L]
Also, it might be worthwhile to add the QSA flag, since this will also preserve any query string that is passed in the original URL:
RewriteRule ^blog/([0-9]+)$ blog/index.php?art=$1 [L,QSA]
I have a search form that on submit generates something like this url:
http://mydomain.com/index.php?find=some+text
And I'm aiming to make it look like:
http://mydomain.com/find/some+text
How can I do this with .htaccess ?
So far I have this:
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} &?find=(.*)&?
RewriteRule ^index.php$ find/%1? [R=301,L]
RewriteRule ^find/(.*)$ index.php?find=$1 [L]
This works if the query (i.e what I search for) contains only numbers, letters or underscore, but I want to make it capable of search for anything including spaces and other characters!
So, it seams that the 404 error was being cause because of some configuration in the web server that did not permit URLs to have + (plus sign).
It would break at the first + and try to find a file by that name.
Having that sorted out the rewrite rules were like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /*.index\.php\?find=([^&\.]+)?\sHTTP
RewriteRule ^/?index.php$ /find/%1? [L,R=301]
RewriteRule ^/?find/(.*)$ /index.php?find=$1 [L,NC]
Thanks for your help!
Shouldn't something like this work?
RewriteRule ^find/([^/]*)$ /index.php?find=$1 [L]
You need to match against the actual request and not the URI because the URI gets rewritten via your second rule. This causes a redirect loop.
To redirect the browser:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?find=([^&\.]+)(&([^\ ]+))?
RewriteRule ^/?index.php$ /find/%1?%3 [L,R=301]
To internally rewrite it back
RewriteRule ^/?find/(.*)$ /index.php?find=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_URI} !(images)
RewriteRule ^(.*)\.(txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$ http://%{SERVER_NAME}/~abc123/uploader/process.php [nc]
Is there any way of not having to quote ~abc123/uploader but to modify my regular expression so that the request_uri without the filename is dynamically passed through?
I've tried looking at removing the filename from the request_uri and also changing my regular expression but to no avail.
What I'm trying to do is to make sure certain file types are processed by a PHP script and cannot be accessed directly.
Here are two clues that might help you:
First, you can use those keywords into your regular expression (I let you google for more information) : %{SCRIPT_FILENAME} and %{REQUEST_URI}
Second, here's an example of how to use it:
# (1) if domain name is static:
RewriteCond %{HTTP_HOST} ^(.*)\.s\.(.*) [NC,OR]
RewriteCond %{HTTP_HOST} ^(.*)\.static\.(.*) [NC]
# (2) and it's not the JavaScript directory:
RewriteCond %{REQUEST_URI} !^/js/(.*)$
# (3) *always* add /templates/ (if not there):
RewriteRule /(templates/)*(.*) /templates/$2 [L]
And a few people know that you can even change the whole destination filename this way (note: the STATIC and PATH_LOCAL variable is an environment variable that I've calculated a few steps before):
# If static...
RewriteCond %{ENV:STATIC} 1
# ...first test to see if the file exists in the language path:
RewriteCond %{DOCUMENT_ROOT}/%{ENV:PATH_LOCAL}/%{ENV:EXT}%{REQUEST_FILENAME} -f
# It exists => rewrite filename then end:
RewriteRule ^(.+) %{DOCUMENT_ROOT}/%{ENV:PATH_LOCAL}/%{ENV:EXT}%{REQUEST_FILENAME} [QSA,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(?!images)(.*)/[-\w\.]*$
RewriteRule ^(.*)\.(txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$ http://%{SERVER_NAME}/%1/process.php [NC,L]
will do what you ask. The (?!...) is a negative lookahead i.e don't include images. The [-\w.]*$ bit will match normal file names. What I don't understand is that a http://%{SERVER_NAME}/... redirect without the [R] is just a normal internal rewrite why you don't just do
RewriteRule ^(?!images)(.*)/[-\w]*\.(?:txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$ $1/process.php [NC,L]
Without any conds or
RewriteCond %{REQUEST_URI} !^images
RewriteCond %{REQUEST_URI} (txt|rtf|docx?|odt|pdf|bmp|gif|jpe?g|png|tiff?)$
RewriteRule ^(.*)/.* $1/process.php [NC,L]
if you want to keep it easier to understand.