I need to write optimized .htaccess rules. I had written like that and they are working
RewriteEngine on
RewriteRule ^city1-name-in-url/$ products.php?city=city1-name-in-url
RewriteRule ^city2-name-in-url/$ products.php?city=city2-name-in-url
RewriteRule ^city3-name-in-url/$ products.php?city=city3-name-in-url
RewriteRule ^city4-name-in-url/$ products.php?city=city4-name-in-url
And url for these rules are http://www.example.com/global/city1-name-in-url/
I have to write these rules for 800 cities which is making website slow. I want to know if there is anyway to get the part url and use it in RewriteRule
Example like
If url : http://www.example.com/global/any-city-name/
RewriteRule ^any-city-name/$ products.php?city=any-city-name
This is possible? To get the part of url after global and then use it in rewrite rule.
You can use this rule in /global/.htaccess:
RewriteEngine On
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ products.php?city=$1 [L,QSA]
[\w-]+ matches 1 or more of [a-zA-Z0-9_-] characters.
Related
I am attempting to rewrite the URL on my website using the .htaccess but have had no luck so far. I want to add a hash to the URL and redirect.
I want to get the last file in the URL and redirect it to the same URL but append a # symbol before the last file. The reason I want to do this is for my website, all the content is loaded dynamically without refreshing the page.
For example,
www.example.com/foo would become www.example.com/#foo
or
www.example.com/form/bar.php would become www.example.com/form/#bar.php
I don't mind if I need one entry for each page, I have tried many variations but nothing has worked so far.
RewriteRule ^(.*)foo(.*)$ $1#foo$2 [R=301,L]
RewriteRule ^(.*)/(.*)$ /$1#$2 [L,R=301,NE]
Have it this way in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*/)([^/]+/?)$
RewriteRule ^ %1#%2 [L,NE,R=301]
A note about RewriteCond %{REQUEST_URI} ^(.*/)([^/]+/?)$:
We are using 2 capture groups in regex here:
(.*/): Match longest match before last / being represented as %1 later
([^/]+/?): Match last component of URI being represented as %2 later
In target we use %1#%2 to place a # between 2 back references.
something like this...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/* /#$1 [NC]
I have a shop URL like this
https://domain.eu/cz/lp/9200
And I'm trying to rewrite it in .htaccess to https://domain.eu/lp/index.php?id=$2&lang=$1
I came really close with
RewriteRule ^/?(hr|sk|pl|cz|ro|it)/lp/(\d+)?$ /lp/index.php?id=$2&lang=$1
which works ok but I can't seem to find a way to handle the situation when there is no lang in URL.
So this is also valid: https://domain.eu/lp/9200 but in that case I want $1 to just be empty (or have a default value when it's not present)
I know "?" means "one or zero" times that's why I tried
RewriteRule ^/?[(hr|sk|pl|cz|ro|it)?]/lp/(\d+)?$ /lp/index.php?id=$2&lang=$1
But it doesn't work as expected. Any point in the right direction would be appreciated.
With your shown samples, attempts; please have your htaccess Rules file in following manner. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rewrite rule for uris which have only 1 parameter.
RewriteRule ^lp/(\d+)/?$ /lp/index.php?id=$1 [NC,L]
##Rewrite rule for uris which 2 parameters.
RewriteRule ^(hr|sk|pl|cz|ro|it)/lp/(\d+)/?$ /lp/index.php?id=$2&lang=$1 [NC,L]
OR use following solutions, in case uris you are trying to access are non-existent ones. Make sure either use 1st solution OR this one, once at a time only.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^lp/(\d+)/?$ /lp/index.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(hr|sk|pl|cz|ro|it)/lp/(\d+)/?$ /lp/index.php?id=$2&lang=$1 [NC,L]
I have an application on Codeigniter(3.1.11). Basically, I want a 404 redirect of URLs which have dashboard in URI. Like these:
dashboard/foo/bar
dashboard/foo-2/bar-2
dashboard/something
...
Also, I want to keep some exceptions in redirect rule so, some of the specific URLs which have dashboard as path URI should be excluded from this redirect. Let's say I want to exclude some URLs like:
dashboard/new-foo/new-bar
dashboard/one-thing/abc
dashboard/another-thing/xyz
I have given a few tries but the exclude rule is not working. It redirects all URLs to 404. This is what I have in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(dashboard/new-foo/new-bar) [NC] # Exclude this url (this condition is not working)
RewriteRule ^(.*)$ dashboard/$1 [R=404,L]
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It can be done in a single rule with negative lookahead conditions like this:
RewriteEngine on
RewriteRule ^dashboard(?!/new-foo/new-bar|/one-thing/abc|/another-thing/xyz) - [R=404,NC,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I'm not expert with htaccess but I am pretty sure you need 2 RewriteCond lines, one to include all the dashboard urls and then the second to exclude the new ones. Each RewriteCond is implicitly joined with an "AND" so if you have many different patterns to exclude and you need a 3rd RewriteCode then you will need to join the 3rd with an "OR" on the 2nd condition.
e.g.
RewriteCond %{REQUEST_URI} ^/dashboard/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/dashboard/new-(.*) [NC]
RewriteRule ^(.*)$ 404-page/$1 [R=404,L]
There are a couple of other things I'd like to mention: 1) Your RewriteRule redirects back to a /dashboard URL so you could possibly end up in a continuous look here. 2) You don't need to turn on Rewrite engine twice, once at the top is enough.
If the rewrite rules in htaccess are getting complicated then perhaps you could use your index.php file to handle it (or some other method within Codeigniter).
Im trying to both remove .php extensions. So for example "http://localhost/timetable/login" instead of "http://localhost/timetable/login.php"
But also have
"http://localhost/timetable/38/" instead of
"http://localhost/timetable/index.php?week=38"
Im able to get one or the other working but not both at the same time. Im assuming its because there is a conflict between them but Im not advanced enough to find it.
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([0-9]+)$ index.php?week=$2
RewriteRule ^([0-9]+)/$ index.php?week=$2
If in the address bar I type "http://localhost/timetable/38" it brings me to "http://localhost/38/" and an Object not Found error.
Does anyone know what the problem is ?
UPDATE: I can now go to the page but
echo $_GET['week'];
Is returning empty result, so its ignroing the 40 in "http://localhost/timetable/40"
Instead of using separate rewrite rule for each input, you should consider routing all of them as a single string to some php file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [L,QSA]
In you php file, you can then separate the string as input and use them as required.
<?php
$inputs = explode('/', $_GET['page']);
You only have one capture group when you try to get the week. So it should be $1 instead of $2.
According to this test tool, the following should work:
RewriteRule ([0-9]+)/?$ index.php?week=$1
I would do something like this:
# rewrite if url ends with a number and possibly a slash
RewriteRule ([0-9]+)/?$ index.php?week=$1 [QSA,L]
# do not append .php if it already ends with .php, other add .php
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [QSA,L]
I have two url's I'm trying to rewrite, for the past... 4-5 hours (headache now).
I am trying to rewrite
/arts/tag/?tag=keyword
to
/search/art?keywords=keyword
Looking at other questions I formulated my rewrite like this
RewriteRule /arts/tag/?tag=([^&]+) search/art?keywords=$1 [L,R=301,NC]
and
RewriteRule ^arts/tag/?tag=$ /search/art\?keywords=%1? [L,R=301,NC]
I tried with backslashes and without, no luck.
Also tried
RewriteCond %{QUERY_STRING} /arts/tag/?tag=([^&]+) [NC]
RewriteRule .* /search/art\?keywords=%1? [L,R=301,NC]
The second one is similar,
/arts/category?id=1&sortby=views&featured=1
to
/art/moved?id=1&rearrange=view
The reason I change the get variable name is for my own learning purpose as I haven't found any tutorials for my purpose. I also changed category to moved since the categories have changed and I have to internally redirect some ID #'s.
RewriteCond %{QUERY_STRING} id=([^&]+) [NC] // I need the path in there though, not just query string, since I'll be redirecting /blogs/category and /art/category to different places.
RewriteRule .* /art/moved/id=%1? [L,R=301,NC]
Any help will be appreciated. Thank you.
Assuming the queries in the original URLs have nothing in common with those in the substitution URLs, maybe this will do what you want, using the first keyin the query as a condition and to identify the incoming URL:
RewriteEngine On
RewriteBase /
# First case
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} \btag\b
RewriteRule .* http://example.com/search/art?keywords=keyword? [L]
Will map this:
http://example.com/arts/tag/?tag=keyword
To this:
http://example.com/search/art?keywords=keyword
# Second case
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} \bid\b
RewriteRule .* http://example.com/art/moved?id=1&rearrange=view? [L]
Will map this:
http://example.com/arts/category?id=1&sortby=views&featured=1
To this:
http://example.com/art/moved?id=1&rearrange=view
Both are mapped silently. If the new URL is to be shown in the browser's address bar modify the flags like this [R,L]. Replace R with R=301 for a permanent redirect.