Htaccess, letters, numbers and spaces - .htaccess

I'm haivng trouble setting up a htaccess rule to follow this convention
www.siteurl.com/item/[NUMBERS][SPACE][LETTERS]/
eg www.siteurl.com/item/82 chocolate
which rewrites to
www.siteurl.com/index.php?page=home&i=$1
EDIT 11-05-16 18:00
RewriteRule ^item/([A-Za-z0-9-]+)/?$ index.php?page=home&i=$1 [NC,L]

You need to use the \s char inside regex character-class to allow space(s) in the uri
RewriteRule ^item/([A-Za-z0-9-\s]+)/?$ index.php?page=home&i=$1 [NC,L]

Related

redirect 301 from underscore to hyphen

I am looking for the right syntax to replace a part of a URL with an underscore to a hyphen
The URL looks like this: www.test.com/variablepart/fixedpart_variablepart/
and I want it to be
www.test.com/variablepart/fixedpart-variablepart/
So I'm looking for fixedpart_ to be replaced with fixedpart-
I tried several things, eg. RewriteRule ^/fixedpart_?$ /fixedpart- [R=301,L]
but it does not work
Try this
RewriteRule ^(variablepart/)(.*)_(.*)$ /$1-$2 [R=301,L,NC]
Make you can add this before all redirect rules.
Note: if you one underscore in rules and redirect to one hyphen. This is for reference.
RewriteRule ^(.*)_(.*)$ /$1-$2 [R=301,L]

url length decrease from htaccess

This is what I am going to do:
If user search
https://englishact.com/Quotation/Every+man+must+do+two+things+alone.html
htaccess will display (only five character)
https://englishact.com/Quotations/allquot.php?quotation=Every
I am trying it in htaccess but not working:
RewriteRule ^Quotation/([.]{5})(.*)\.html$ Quotations/allquot.php?quotation=$1 [NC,L] # Handle product requests
Will be helpful if you can give me edit in the code above.
You have an issue in your RegEx. [.] will match exactly a . instead if any character since the . is wrapped in [].
Try the below rule. It should work.
RewriteRule ^Quotation/(.{5})(.*)\.html$ Quotations/allquot.php?quotation=$1 [NC,L] # Handle product requests
To capture only the first five letters after Quotation/ and before the + sign you can try this:
RewriteCond %{REQUEST_URI} ^/?quotation/([a-z0-9.]{5}).*\.html$ [NC]
RewriteRule ^(.*)$ /quotations/allquot.php?quotation=%1 [NC,L]
it will accept the first five characters of the query string if they are characters between a-z (case insensitive because of NC flag),numbers 0-9 or a period.

Edit existing .htaccess to work with subdirectories too

I have an existing .htaccess file that looks like this:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^posts/(.*)/(.*).html$ index.php?content=blog&postid=$1&kwd=$2
RewriteRule ^aposts/(.*)/(.*).html$ index.php?content=autopost&postid=$1&kwd=$2
RewriteRule ^category/(.*)/(.*).html$ index.php?content=category&cat=$1&otext=$2
RewriteRule ^content/(.*).html$ index.php?content=$1
RewriteRule ^searching/(.*).html$ index.php?content=search&k=$1
RewriteRule ^related/(.*).html$ index.php?content=related&k=$1
RewriteRule ^blog/(.*)/(.*).html$ index.php?content=blog&postid=$1&kwd=$2
RewriteRule ^posts$ index.php?content=blog
RewriteRule ^sitemap\.xml$ sitemap.php [L]
RewriteRule ^robots\.txt$ robots.php [L]
ErrorDocument 404 /404.php
This works how it was initailly intended for root domain address visitors.
What I want now is to add the functionality of accepting directories too, for example:
example.com/directory1
or
example.com/dir1/dir2/dir3/dir4
but I want it to keep the same functionality as it currently does.
I hope that makes sense, but feel free to ask for clarification.
Thanks for anyhelp with this.
RewriteRule ^posts/(.*)/(.*).html$ index.php?content=blog&postid=$1&kwd=$2
This rule of yours always only matches when the URI starts with "posts".
To allow one or more subdirectories before such a rule, you want to look for one or more characters followed by a / before "posts":
(.+/)? this selects "one or more of any characters, followed by a /", zero or one times. Since "any character" also includes a slash this counts for any amount of subdirectories, as long as after the last slash the "posts" appears.
Since an extra set of parentheses is introduced, you will need to access $2 and $3 now.
So your rule becomes:
RewriteRule ^(.+/)?posts/(.*)/(.*).html$ index.php?content=blog&postid=$2&kwd=$3
You can apply this to each of your rules.

allow Special Characters in URL

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

Do you have to escape a forward slash when using mod_rewrite?

With regards to the forward slash "/" when giving a regex to RewriteRule or RewriteCond, or anything else related to .htaccess in particular, is there a need to escape the forward slash?
Here is an example of what I am trying to achieve
RewriteEngine on
RewriteOptions inherit
RewriteBase /uk-m-directory/
RewriteRule ^(region|region\/|regions\/)$ regions [R=301,L]
RewriteRule ^(county|county\/|counties\/)$ counties [R=301,L]
RewriteRule ^(city|city\/|cities\/)$ cities [R=301,L]
The above works fine, and it continues to work fine when I remove the backslashes as shown below
RewriteEngine on
RewriteOptions inherit
RewriteBase /uk-m-directory/
RewriteRule ^(region|region/|regions/)$ regions [R=301,L]
RewriteRule ^(county|county/|counties/)$ counties [R=301,L]
RewriteRule ^(city|city/|cities/)$ cities [R=301,L]
Which one is the correct way? Are they both wrong?
Is there any special reason the forward slash should be escaped, or shouldn't?
My guess is that the forward slash does not need to be escaped because it isn't a special character, as far as I know. But I just want to be sure.
In case you're wondering the point of this code, it redirects city, county, and region (with or without a forward slash) to their plural equivalents. Furthermore if the plural has a forward slash it removes the forward slash.
No, you do not have to escape slashes. Forward slashes don't have any special meaning in regular expressions.
The one common character that has bitten me in the past is ? in query strings. That one you do have to escape.

Resources