Here I got such url
http://minecraftstorage/add
And this rule
RewriteRule ^add/? add.php [L]
But that rule affects not only add string which is in URL, but also additionals string which is in the path to Bootstrap css.
So how can I make it to work only for add and not for additionals?
Change to RewriteRule ^add/?$ add.php [L]
$ denotes the end of a regex (regex = regular expression) test string.
Related
I want to change the URL of my site:
http://example.com/clinicdetails.php?url=/diet-clinic-in-punjabi-bagh.html
to look like this:
http://example.com/clinicdetails.php/diet-clinic-in-punjabi-bagh.html
My code in .htaccess file is as follows:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /clinicdetails.php?url=$1 [L]
However, this is not working (no error) on my localhost and also not on the shared hosting servers. What could be the issue here?
RewriteRule ^([^/]*)\.html$ /clinicdetails.php?url=$1 [L]
The RewriteRule pattern is failing to match the given URL because of the start of string anchor ^. Whilst you don't have slashes in the path segment you are trying to match, you do in the rest of the URL. So, try the following:
RewriteRule ([^/]+)\.html$ /clinicdetails.php?url=/$1 [L]
I've also changed the * to +, since the filename is at least 1 char. A more specific regex is better.
To map to your desired URL, you would also seem to require a slash prefix on the URL param value. ie. url=/$1
Let's say I have phpbb3 forums software and I want to prettify some URLs. I put this in my htaccess:
RewriteRule ^cake viewforum.php?f=5&%{QUERY_STRING} [L]
which works for domain.tld/cake and domain.tld/cake/ but it also catches domain.tld/cake-recipes and domain.tld/cake-recipes/ for instance, and so rewrites them.
How can I write this so that it only matches that exact URL, not URLs that begin with that string?
You need to add $ in order to delimit your rule pattern.
RewriteRule ^cake/?$ viewforum.php?f=5 [L,QSA]
The above rule will now only match domain.tld/cake or domain.tld/cake/.
Also, you can avoid using %{QUERY_STRING} by adding QSA flag (which does the same, but in a more elegant way)
I am currently having a problem with my index url rewrite in my .htaccess file, I know if I use
RewriteRule ^profile/([^/]*)/?$ /profile.php?x=$1 [L]
I would be able to use www.example.com/profile/get or www.example.com/profile/get/ (with or without trailing slash)
But I would like www.example.com/get what I have so far is
RewriteRule ^([^/]*)\/$ /index.php?x=$1 [L]
But if I put a ? before the $ it errors any answers welcome
Making the trailing slash optional will lead to an infinite loop, since [^/]* will match anything that doesn't include a /, ie it would also match index.php?x=get
You can avoid this by making the rule apply conditionally, for example by testing the reqeust URI:
RewriteCond %{REQUEST_URI} !^/index\.php.*
RewriteRule ^([^/]*)\/?$ /index.php?x=$1 [L]
That way the rule can only apply in case the request URI doesn't start with /index.php
I want to check if a URL contains the sting "-EN.htm", if so apply the rewrite.
That should be done with ^-EN.htm as follows, but the rule is not working:
RewriteCond %{REQUEST_URI} ^/(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm
RewriteRule ^(.*)$ /indexEN.php?folder=%1&follow=%2 [L]
What am I doing wrong?
Thank you for every help,
Scott
Your regular expression doesn't look right. You can also lose the condition and just move the pattern to the rewrite rule instead. Something along the lines of
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm /indexEN.php?folder=$1&follow=$2 [L]
You need to make the leading slash optional (in htaccess this is stripped off) and instead of using % backreferences, use the $ ones.
Now on to your pattern, it's not valid. The ^ matches the beginning of the string (the URI), so if you have two of them and you're not trying to literally match the ^ character (which you'd need to escape), then the expression will never match anything. Without any examples of URLs that you're having to deal with, I assume you probably just want to ditch the second ^:
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)-EN.htm /indexEN.php?folder=$1&follow=$2 [L]
I want to allow special charecters in URL
my htaccess code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^blog/?$ SocialNetwork/blog.php [L]
RewriteRule ^blog/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/([A-Za-z0-9-_[]]+)/?$ SocialNetwork/blog.php?id=$1&title=$2 [L]
</IfModule>
it works for
blog/1
blog/1/hii
blog/1yooo_title
but does not work for
blog/1/[hii-this is [] title
In your last rewrite rule, try escaping the square brackets. Also if you need to match spaces you need to include \s in there:
^blog.php/(\d+)/([A-Za-z0-9-_\[\]\s]+)/?$
Or perhaps consider the simpler which accepts anything in the title:
^blog.php/(\d+)/(.+)/?$
Another point is that you should not have spaces in your URLs. They should be escaped to "+" or %20. So depending on this your regex would change, except the last one I proposed should work.
I'm pretty sure that you can't use spaces in a URL. Ever.
only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.
http://www.ietf.org/rfc/rfc1738.txt