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]
Related
I am totally new to this field.I would like to change this url
http://localhost/livelor/profile.php?username=naveen471996
http://localhost/livelor/naveen471996
Yes i did that When url like this is not working 404 Not found
http://localhost/livelor/vikaass.waran
This my .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteRule ^([0-9a-zA-z]+)$ profile.php?u=$1 [L]
RewriteRule ^([0-9a-zA-z]+)
means that just the chars 0-9 a-z and A-Z are allowed
RewriteRule ^([0-9a-zA-z.]+)
should solve the problem and you can use DOT's in the username.
You should have a look at REGEX's
It returns a 404 because it thinks that .warran is the file extension, and fails to load it. To fix this problem, either ban periods (aka dots) from the usernames or use % encoding (http://php.net/manual/en/function.urlencode.php):
$final_url = urlencode($username);
Then, when you want to decode it, use:
$user = urldecode($_GET["username"]);
That way, you'll be able to pass most UTF-8 characters via the $_GET. Be careful about HTML characters (and % codes decodable into HTML characters), though, since that might lead to some hacker issues. Use preg_replace() or something similar to get rid of those before you decode the url.
For a complete list of % codes, visit http://www.w3schools.com/tags/ref_urlencode.asp
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/
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
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).
Is there a way I can use PHP (and/or .htaccess) to rewrite the URL of a page.
For example if a user goes to www.mysite.com/french they are actually accessing the page which is www.mysite.com/index.php?lang=fr
But not a redirect.
You want to use mod_rewrite and an .htaccess file to achieve this.
RewriteEngine On
RewriteBase /
RewriteRule ^french/(.*)$ /index.php?lang=fr [L,QSA]
Yes, using Apache mod_rewrite and appropriate rules in an .htaccess file.
The docs on mod_rewrite are here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
on the Apache site you can find several examples of URL rewriting flavors, by the way it's enough to use something like this in an .htaccess file:
RewriteEngine on
RewriteRule ^french(/|)$ /index.php?lang=fr [flag]
Where [flag] can be one of the following:
[PT]
[L,PT]
[QSA]
[L,QSA]
You may want to have a look at the PT (passthrough) flag docs or RewriteRule flags docs.
Also, pay attention to what your links are pointing to: in fact, the RewriteRule first argument is a regular expression that will be used to match the URLs to be rewritten. At the moment,
^french(/|)$
matches "french", right after the domain name,followed either by a slash (/) or nothing (that's the meaning of (/|) ); that is, it'll match www.mysite.com/french and www.mysite.com/french/ but nothing else. If you need to parse query string arguments, or subpaths, then you may need a more complex regex.