URL Rewrite behaving strange - .htaccess

Hi i have one of rewrite rules as below
RewriteRule ^([^/.]+)/?$ /cityres?city=$1 [L]
This makes URL like http://example.com/value-of-city and this works perfectly fine.
But here problem i am facing is that if user types
`http://example.com/anyrandomvalue`
then URL behave same as
`http://example.com/value-of-city`
and tries fetching results based on anyrandomvalue
How can i avoid same?

You can restrict your regex to some allowed characters like a word character and hyphen:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ /cityres?city=$1 [L,QSA]

Related

.htaccess rewrite with multiple parameters

I had a rule in my .htaccess that makes URLs for articles more friendly looking for the purposes of SEO and the like.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/(.+).php /$1/article.php?title=$2 [L]
Which converted this URL:
/news-and-views/going-for-brokering.php
To this within the application itself:
/news-and-views/article.php?title=going-for-brokering
Now I need a URL with an ID before the title like this:
/news-and-views/123456789/going-for-brokering.php
So I tried the following rule:
RewriteRule ^(news-and-views)/(.+)/(.+).php /$1/article.php?Id=$2&title=$3 [L]
However, this isn't working, am I misunderstanding the use of the brackets as I thought everything between them was acknowledged as a variable on the right-hand side?
I'm thinking it could even be that the less specific rule is above the more specific rule.
You need to be careful about the order of the rules, since your first rule will also match /news-and-views/123456789/going-for-brokering.php. Change your rules as follows:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/([^/]+).php /$1/article.php?title=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/([0-9]+)/([^/]+).php /$1/article.php?Id=$2&title=$3 [L]

htaccess filter doesn't work

i'm trying to rewrite my url's, but i'm also using a router.
so i want the router to control error pages such as error 404 not found, and not my htaccess file. everything is working perfectly fine, except for the filter string in my htaccess file. this is the rewrite line:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-\_]+)$ index.php?urlpath=$1
so then issue is that it doesn't accept characters like . and /
so this works: https://www.mystem.tk/aurlthatdoesntexist
but this won't work: https://www.mystem.tk/apagethatdoesntexist.php
and this won't work either: http://www.mystem.tk/a/url/that/doesnt/exist
last one: https://www.mystem.tk/a/page/that/doesnt/exist.php
i hope my problem is clear on this way... you can check it out yourself at https://www.mystem.tk/
This is due to your regex [a-zA-Z0-9-z\-\_]+ which doesn't allow dot or forward slashes.
You can use this rule instead of your existing rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ index.php?urlpath=$0 [L,QSA]

.htaccess conflicting rewrite rules

I'm trying to change a website to multi-language, so I have URL's like this:
www.company.com/en/about
www.company.com/fr/about
which should point to index.php?lang=en&what=about
so I defined the following rewrite rule (which works)
RewriteRule ^en/(.*)$ ?lang=en&what=$1 [NC,L]
RewriteRule ^fr/(.*)$ ?lang=fr&what=$2 [NC,L]
but I also need the homepage url as www.company.com/en (pointing to index.php?lang=en)
which does not work for this rule.
The best solution would be something like
RewriteRule ^(.*)/(.*)$ ?lang=$1&what=$2 [NC,L]
but it converts all the urls, like href='css.css' kind of references, so it messes up the whole page.
so how should I restrict the first GET variable to be two chars? or one of the defined languages?
Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})(?:/(.*)|)$ /?lang=$1&what=$2 [L]
The first grouping, ([a-z]{2}), captures the 2 letter language. The second optional grouping captures the "what". If there's nothing there, then "what" will be blank.

.htaccess case sensitive and mod_rewrite

My .htaccess:
RewriteEngine On
CheckCaseOnly On
CheckSpelling On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^Blog/(.*?)$ /Me/profile.php?username=$1 [QSA,L]
The problem is, when the URL is like this, it works:
localhost/Me/Blog/ExampleUser
But it doesn't work when it is like this (notice the 'b' in 'Blog' is in lowercase?):
localhost/Me/blog/ExampleUser
I'm running it on the new version of XAMPP. It is wierd its not working even though I have the mod_speling.so on the PHP config.
What is the problem?
Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.
Try [QSA,L,NC] instead, so the comparison is made in a case-insensitive manner
the problem is that rewrite rules ARE case sensitive. So your Rewrite rule should be:
RewriteRule ^[Bb]log/(.*?)$ /Me/profile.php?username=$1 [QSA,L]
and voila you are fixed.
mod_speling.so has NOTHING to do with this.

htaccess directory to file redirect problem

I’m trying to use the following .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^images/
RewriteRule (.*) view.php?picid=$1 [L]
RewriteRule ^/user/(.*)$ /users.php?user=$1
I want two things to happen: Whenever someone requests /1234, it redirects to /view.php?picid=1234, and also when someone visits /users/bob, it redirects to /users.php?user=bob.
My code however, doesn’t seem to be working correctly.
There are several ways to do that. Here’s one that should work:
RewriteRule ^user/(.+)$ users.php?user=$1 [L]
RewriteRule ^([0-9]+)$ view.php?picid=$1 [L]
The first rule will catch any request that’s URI path begins with /user/ followed by one or more arbitrary characters. And the second will catch any request that’s URI path begins with / followed by one or more digits.
The initial problem with your rules is that the RewriteRule with (.*) will match everything.
If you do not want it to match a URL with a slash in it (such as users/bob), try ^([^/]*)$
Secondly, after a URL is rewritten, the new URL goes through your rules again. If you want to avoid matching something that has already been rewritten once, you should add a condition like
RewriteCond %{REQUEST_URI} !\.php

Resources