URL rewriting with spaces - php

I have a url like this :
www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe
I am trying to conver this like below with the help of .htaccess file :
www.qwerty.in/1/abcd-cafe
I am trying the below but unfortunately its not working . can anyone help
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ details.php?vendor_id=$1&name=$2

You character must allow space as well:
RewriteRule ^([\w-]+)/([\w-\s]+)$ details.php?vendor_id=$1&name=$2 [L,QSA]
I have used \w, which is same as [a-zA-Z0-9_].

The following rule will convert your URL from www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe to www.qwerty.in/1/abcd%20cafe:
RewriteRule ^([^/]*)/([^/]*)\.html$ /details.php?vendor_id=$1&name=$2 [L]
It it much more generalized however, and will convert the first two parameter values even if they are not vendor_id or name. If this is not the behavior you'd prefer, have a look at anubhava's answer.
Converting a space to underscore can be achieved using:
RewriteRule ^(.*)\s(.*)$ $1_$2 [N]
Also, don't forget to ensure the rewrite engine is online:
RewriteEngine On
If you want to further experiment with these rules, I'd recommend a website I recently discovered: http://www.generateit.net/mod-rewrite/

Related

Simple pretty urls with .htaccess

I have a one page blog php website.
Content is dynamicaly loaded based on get parameters. I would like to use my htaccess to make pretty urls. I have these urls:
website.com/index.php?category=review&page=1
And I would like to have this:
website.com/category/review/page/1
And I also use article as get parameter. So I would like to change this:
website.com/index.php?article=12345-name-of-article
To this:
website.com/article/12345-name-of-article
I am totally new to htaccess, so any help would be appreciated.
I tried this rewrite rule:
RewriteRule ^article/([0-9a-zA-Z_-]+)$ index.php?article=$i [NC,L].
It worked somehow, but php script does not recognize url parameters. So it does not work.
Thank you very much!
You need to use QSA - When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined. :
RewriteEngine On
RewriteRule ^article/([\w-]+)(?:\.html|/)?$ index.php?article=$1 [NC,QSA,L]
RewriteRule ^category/([\w-]+)/page/([\w-]+)(?:\.html|/)?$ index.php?category=$1&page=$2 [NC,QSA,L]

Transform Regex to RewriteRule

A few minutes ago I asked a question and received a very good response. It works. (This is it). I added it to my PHP scripts. Now I need to complete my site with another question like that but little characterized. I need to get rewrite rule from that regex (it's different problem, not my previous).
I need to rewrite link like:
http://my_site.com/technic/k-700/?type=repair
to link like:
http://my_site.com/repair/k-700/
Instead of k-700 can be any another combination (between / ) and instead of repair can be only kit.
Now I need rewrite rule for .htaccess file. Please.
My result is not working:
RewriteRule ^([^\/].)/k-700/$ /technic/k-700/?type=$1 [L]
I can't avoid that k-700 :(
If my understanding is correct and this is going in your root .htaccess, then try this:
RewriteRule ^(repair|kit)/([^/]+)/?$ technic/$2/?type=$1 [L]
UPDATE: Re-read your description. a little ambiguity with the repair vs kit value and whether substitution should always have k-700. So here are a few more examples catering to those possibilities:
RewriteRule ^(repair|kit)/([^/]+)/?$ technic/k-700/?type=$1 [L]
RewriteRule ^(kit)/([^/]+)/?$ technic/k-700/?type=$1 [L]
RewriteRule ^(kit)/([^/]+)/?$ technic/$2/?type=$1 [L]

Confusion with .htaccess

I'm trying to wrap my tiny brain around how the .htaccess can convert my somewhat undesirable URL into a cleaner eye candy link.
This is my current, scruffy URL.
expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
And what I would like it to be render as, would be something perhaps like this?
expand/Mods/Wrestlers/WWE/
Am I right in thinking this is correct syntax to perform this? Because it doesn't seem to do anything right now!
RewriteRule ^([^/]+)/([^/]+)$ expand.php?p=$1&sp=$2 [L]
I would appreciate it if some bright chap might be able to help me out of this pickle!
You can do it like this. You will need to make sure the rewrite matches the URL you wish to internally redirect too, which is expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
So to take care of that you should be able to use this.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+) expand.php?category=$1&subcategory=$2&faction=$3 [NC,L]
Try
^expand/(\w+)/(\w+)/(\w+)?$ expand.php?category=$1&subcategory=$2&faction=$3
To internally rewrite expand/Mods/Wrestlers/WWE/ to expand.php?category=Mods&subcategory=Wrestlers&faction=WWE, you'll need to make a regex that matches that first url. Your current regex would match an url with 2 parts, but your example url has 4 parts. I am pretty sure that doesn't fit ;-)
So... how do we fix it? Well, we make a regex with 4 parts. In fact, we know that the first part needs to be equal to expand, so we end up with this:
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+)/?$ /expand.php?category=$1&subcategory=$2&faction=$3 [QSA,L]
If you add this to the .htaccess in your www-root and we now go to http://example.com/expand/Mods/Wrestlers/WWE/, we should see whatever expand.php outputs with those parameters.

Detect Single Segment RewriteRule apache

I'm stuck in a very basic thing, I'm try to redirect a single segment url to a page
RewriteRule /([A-Za-z0-9]+)$ index.php?c=profile&val=$1 [NC,L]
this works fine for URLs like
sitename.com/myurl
But the problem is its also valid for URLS like
sitename.com/myurl/something
or
sitename.com/myurl/something/x/y/z
I want it to only work for single segment after hostname, if I use it with ^ sign at the start it don't work at all
Any help would be greatly appreciated.
Thanks
You still have to use ^, but must leave out the leading /
RewriteRule ^([A-Za-z0-9]+)$ index.php?c=profile&val=$1 [NC,L]
For compatibility with older mod_rewrite versions you often see idioms like ^/?. But current Apaches mod_rewrite strips the leading slash already (from the REQUEST_URI which RewriteRules operate from primarily).

mod_rewrite accept only dashes & alphanumeric, otherwise throw 404

I'm using mod_rewrite to transfer the end of the URL to my php script which will act as a microcms. My url will look something like this:
mysite.com/articles/some-article-about-css
That will pass the following to my PHP script index.php:
index.php?action=read&article=some-article-about-css
I have questions:
I'm unsure how the best way to only accept alphanumeric w/ dashes using mod_rewrite. I found this rule on the internet ((?:[a-z]+)?(?:[0-9]+)?-?)+ and it apparently doesn't allow double dashes which is even better, but its long and confusing. Is there another (shorter, faster) rule I can use?
I'd like to verify that this rule is valid for what I'm trying to accomplish. I'm not very good with mod_rewrite:
RewriteRule ^/articles/((?:[a-z]+)?(?:[0-9]+)?-?)+ /index.php?action=read&article=$1
What rule can I use so that if the url after /articles/ is not a valid alphanumeric /w dashes url, automatically throw a 404 rather than passing to my script?
RewriteRule ^/articles/([a-zA-Z0-9-]*) /index.php?action=read&article=$1 [QSA,L]
RewriteRule ^/articles/ - [R=404]
For alphanumeric and dashes only
You dont need that complex regex i believe. Try this
RewriteRule ^articles/([^a-zA-Z0-9-]*) /index.php?page=$1 [L]
RewriteRule ^articles(.*) [R=404]

Categories