How can I show following URL using .htaccess re-write rule with a non ? parameter?
Original URL
http://example.com/index.php?store=1
Desired URL
http://example.com/1/
OR
Desired URL
http://example.com/store/1
I am trying following code in .htaccess. What I am missing here?
RewriteEngine On
Options +Followsymlinks
RewriteCond %{REQUEST_FILENAME} !-f
rewriteRule ^store/(.+)\$ index.php?store=$1 [L]
my index.php file has this code
<?php
$storeid = $_GET['store'];
echo $storeid;
?>
What I am missing here?
You are escaping your end-of-string character, causing the expression to look for a literal $ sign so it never matches.
You need something like:
rewriteRule ^store/(.+)$ /index.php?store=$1 [L]
^ no back-slash here
or if you want to make sure you only match numbers:
rewriteRule ^store/(\d+)$ /index.php?store=$1 [L]
Related
I have a page in wordpress I am using with a slug called Bad-Debt-Recovery-in/. I am using a custom php query on that page with strings in the URL's like this
Bad-Debt-Recovery-in/?zipcode=55555&location=Chambers%20County+AL
How can I make this url into a slug like this
Bad-Debt-Recovery-in/55555/Chambers-County/AL/
as a rewrite? Any help would be appreciated!
UPDATE:
This code is actually what I am using. I also made it simpler and created a third variable named "state". One rewrite is for City and one is for County page:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agencies-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agencies-Services-In/?zipcode=$1&city=$2&state=$3 [QSA,L,NC]
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agency-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agency-Services-In/?countyid=$1&county=$2&state=$3 [QSA,L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
# END WordPress
What you are asking for, and seeking to accomplish is called: Converting a request path into a query string.
Use .htaccess RewriteRule directive to modify the incoming request
RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?a=$1&b=$2&c=$3 [QSA,L,NC]
Each ([^\/]+) captures path elements into a variables $1,$2,$3...
The ? at the end simply denotes that the last / is optional or else the last match could fail
the \ simply escape the '/' for literal interpretation
Add as many ([^\/]+) as needed and capture them in the query
zipcode=$1&location=$2+$3
The modifiers at the end [QSA,L,NC] are called flags
QSA appends the query string to the end of the rewrite if any exist
L simply says this is the last rewrite
NC means not case sensitive
This is your final solution:
RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?zipcode=$1&location=$2+$3 [QSA,L,NC]
You can use the WP function sanitize_title
Combine this with a php loop i.e.
$url = $_SERVER['REQUEST_URI'];
foreach($_GET as $g){
$url .= '/'.$g;
}
$url = sanitize_title($url);
I am trying to re-write the dynamically generated URL. But the rules i have written is not affecting the URL even a single bit.
The URL currently shows up like: http://ukfurniturespecialist.co.uk/county.php?c=Oxfordshire&%20t=Faringdon
And i would like it to look like: http://ukfurniturespecialist.co.uk/county-c-Oxfordshire-t-Faringdon.html
This is what i have tried:
Options +FollowSymLinks
RewriteEngine on
RewriteRule county-c-(.*)-%20t-(.*)\.html$ county.php?c=$1&%20t=$2
Any help is much appreciated,Thanks.
For your URL following rule should work:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+county\.php\?c=([^&]*)&.*?t=([^&\s]*) [NC]
RewriteRule ^ county-c-%1-t-%2.html? [R=302,L]
RewriteRule ^county-c-([^-]+)-t-([^.]+)\.html$ county.php?c=$1&\%20t=$2 [L,QSA,NE]
%20 represents a space character in url. when writing rewrite rule the url is already decoded and handled using "\ " instead of %20
For your URL the rewrite rule should be
Options +FollowSymLinks
RewriteEngine on
RewriteRule county-c-(.*)-\ t-(.*)\.html$ county.php?c=$1&\ t=$2
I dont think you need to put space character in your url. If you remove the %20 from your URL the rewrite rule should be
RewriteRule county-c-(.*)-t-(.*)\.html$ county.php?c=$1&t=$2
I want to redirect URL, using .htaccess. I have the following code:
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?action=$1&id=$2
This is working correctly if I don't use dot in the URL. But if I want to access for example this URL:
load/com.example.unique.id
then I will get error 404. Of course, I can understand why is this: I haven't include the dot in my expression. However if I include it, I will get error 500.
I tried to include dot in the expression like this:
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-\.]+)$ index.php?action=$1&id=$2
Any idea why is this happening? Thanks!
I think the problem is with the '-' in [a-zA-Z0-9_-.], try this:
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_\-.]+)$ index.php?action=$1&id=$2
You can use dot but hyphen needs to be last or at first position in character class to avoid escaping. Correct regex based rule will be like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w.-]+)/([\w.-]+)/?$ index.php?action=$1&id=$2 [L,QSA]
the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies
I have an url with the following format:
domain/product.php?name=product a
Now I need to rewrite this to:
domain/product a (including the space in the word) like:
http://www.directline-holidays.co.uk/Costa Blanca
How do I do this? The closest result I got so far is the following:
domain/p/product-a
With the following code in .htaccess
RewriteEngine On
RewriteRule ^p/([^/]*)/$ destination.php?name=$1
I could not even use the name without the "-". I need the product name just as it is in the database. Is this possible?
This should work: It will simply give you the rest of the URL string after the /p/ directory to the end of the string, which in your case should be the end of the URL, correct?
RewriteRule ^p/(.*)$ destination.php?name=$1
For the pages that are not product pages, if you know they will end in a .php file extension you can filter for those pages with the following rule:
RewriteCond %{REQUEST_URI} !^.*(destination\.php).*$
RewriteRule ^([^\.php]+)$ destination.php?name=$1
EDIT: Fixed for infinite loop condition by adding RewriteCond for destination.php
add "%20" to the URL, such as:
http://www.directline-holidays.co.uk/Costa%20Blanca
20 in hexadecimal base is the ASCII number for space.
Edit:
In addition to powtac's comment:
Use the JS encodeURIComponent() function to encode a value that should be used in a URL:
http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp
Try this rules..
Options +FollowSymLinks
RewriteEngine on
RewriteRule product-name-(.*)\.htm$ product.php?name=$1
or
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/name/(.*)/ product.php?name=$1
RewriteRule product/name/(.*) product.php?name=$1
do you mean if destination is a php file , doesn't rewirte it , else rewrite any thing to destination.php ? if so , this should work
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ destination.php?name=$1