Detect Single Segment RewriteRule apache - php

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).

Related

How to redirect everything after slash to domain before?

This is what I'm trying to do:
I want example.com/car/{whatever} redirected to example.com/{whatever}.
For example, example.com/car/honda should be redirected to example.com/honda
BUT if there is nothing after /car/, then just leave it as that.
How can i do that in .htaccess ?
Removing a prefix from a URL is pretty simple, just
RewriteRule ^car/(.*)$ /$1 [R,L]
In order to keep a special case, you must put it in front
RewriteRule ^car/$ - [L]
This rule says, if there is just car/ leave it alone, see RewriteRule substitution
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.

.htaccess RewriteEngine not working for secondary GET

I'm creating friendly links in my site, but I have a problem. I wrote this, but only the first line of code is working, and I'n not sure why:
RewriteRule ^user/([0-9a-zA-Z]+) user.php?user=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z]+)/options user.php?user=$1&options [NC,L]
When I go to test.com/user/tom everything is fine, but when I try test.com/user/tom/options it just loda the same page.
I have php script that should load different pages, for different $_GET, and its working if used with normal links.
The correct thing to do here is not to swap the lines around. Whilst that may fix your problem, it will also send user/tom/foo/bar to the first rule. The reason it does this is because the pattern is not closed with $. As such, your rules should read as follows:
RewriteRule ^user/([0-9a-zA-Z]+)$ user.php?user=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z]+)/options$ user.php?user=$1&options [NC,L]
Tip: If you also have other pages for the user, you can simplify the second rule to include different segments by means of a capture:
RewriteRule ^user/([0-9a-zA-Z]+)/(options|other-page)$ user.php?user=$1&$2 [NC,L]
Also giving consideration to the fact that you are using the NC (no case) flag, you can drop the A-Z in your capture groups. You can also use \d for digits. Example [a-z\d]+

URL rewriting with spaces

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/

mod_rewrite not working for clean urls

I am using wamp server for my projects and have stuck in making the urls clean
currently my url address is like this
http://localhost/BookProjectFinal/booksdetails.php?pid=8
I want to make it like this
http://localhost/BookProjectFinal/booksdetails/8
What I am using in my .htaccess file is this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ booksdetails.php?pid=$1
RewriteRule ^([a-zA-Z0-9]+)/$ booksdetails.php?pid=$1
But its not working its returning the whole url. What the problem with it? Thanks
The RewriteBase parameter will tell mod_rewrite from which directory to start matching. This can be used for applications that live in subfolders of apache. You may want to set it to /BookProjectFinal/ here.
Your regexp is looking to match a complete alphanumeric URL optionally followed by a slash, which I'm not sure is what you want. Try something like:
RewriteRule bookdetails/([0-9]+) bookdetails.php?pid=$1
to match only the number. Here's a test:
RewriteEngine On
RewriteRule ^bookdetails/([0-9]+) bookdetails.php?a=b&pid=$1
Matches URLs of the form /bookdetails/8. Use print_r($_GET) to sanity check in bookdetails.php

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