Adding trailing slash to URL using htaccess - .htaccess

I'm trying to re-write my URL using htaccess. Here is the code from my htaccess:
RewriteRule ^post/([^/.]+)?/([^/.]+)?$
post.php?pst=$1&view=$2%{QUERY_STRING}
Everything is working fine, except that this code doesn't add a trailing slash at the end of the re-written URL. How can I add that?

I don't think you can access %{QUERY_STRING} in a RewriteRule; only a RewriteCondition. I think you might need to do something like:
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^post/([^/.]+)?/([^/.]+)?$ post.php?pst=$1&view=$2%1/
See: http://wiki.apache.org/httpd/RewriteQueryString

Related

.htaccess url rewriting without collision

I've got the following code in my htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1
The above code works fine like this, http://domain.com/username instead of http://domain.com/profile.php?username=username
I need a similar one but with a fake string like this
http://domain.com/gallery/username
The file is at http://domain.com/gallery.php
How do I achieve this without colliding with profile code?
Almost the same:
RewriteRule ^gallery/([a-zA-Z0-9_-]+)/?$ gallery.php?username=$1
If you also would allow other non ascii nicknames you should replace ([a-zA-Z0-9_-]+) by ([^\/]+).
Just as a hint you can remove the first rule if you add a questionmark after the slash that makes the slash optional.

htaccess redirect with parameters not working

I'm struggling with an Apache rewriterule. I need to do the following:
Redirect permanently:
http://domain.com/folder/viewer/data/settings.xml?prevent_cache=4760
to
http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=4760
I've got the code below, it works without the url parameters but I can't seem to get it to work with parameters. Am i missing something?
RewriteCond %{QUERY_STRING} ^prevent_cache=([0-9]*)$
RewriteRule ^/folder/viewer/data/settings.xml$ http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R=301,L]
Cheers
Shaun
The only error I can see, is the leading slash / in the RewriteRule pattern. This should be
RewriteCond %{QUERY_STRING} ^prevent_cache=[0-9]*$
RewriteRule ^folder/viewer/data/settings.xml$ /siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R,L]
You don't need to append the query string to the substitution URL, because this is done autmoatically.
When everything works as you expect, you can change R to R=301. Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
I can. Here are the rewrite condition and rule that you're looking for:
# once per htaccess file
RewriteEngine on
RewriteCond %{QUERY_STRING} prevent_cache=([0-9]*)
RewriteRule ^folder/viewer/data/settings.xml http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=%1 [R=301,L]
But please considered this answer about the [R=301] flag: https://stackoverflow.com/a/15999177/2007055

Rewrite doesn't work without trailing slash

I have problems getting an URL to work WITHOUT entering trailing slash.
It's:
www.domain.com/shop/buy/products/show/range/
.htaccess Rewrite Rule is:
RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)/?$ _shop/products.php?trg=${productmap:$1}&range=$2 [L]
It works with trailing slash (which I don't want in the URL) but not without. I should also add that if I was to remove '/show/' from the URL (which I can't do though), it works without trailing slash, or if 'range' contains a dash '-', as in 'new-product', it also works.
However, this URL works with or without trailing slash:
www.domain.com/shop/buy/products/show/range/color
.htaccess Rewrite Rule for this URL is:
RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)/([A-Za-z0-9\-\,]+)/?$ _shop/products.php?trg=${productmap:$1}&range=$2&color=$3 [L]
How can I get the first URL to work without trailing slash? This might be something really obvious as I'm a recent newbie to using .htaccess but I have now spent hours staring at the code and reading forum posts about rewrites but not been able to resolve this. Thank you!
T'm not so sure, but have You try to put /? in braces: (/)?, I think this will work:
RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)([/]?)$ _shop/products.php?trg=${productmap:$1}&range=$2 [L]
You Can Test This Code
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^shop/buy/(.*?)/show/(.*)/(.*)$ _shop/products.php?trg=$1&range=$2&color=$3 [S,L,QSA]
RewriteRule ^shop/buy/(.*?)/show/(.*)$ _shop/products.php?trg=$1&range=$2 [S,L,QSA]
sample:
www.domain.com/shop/buy/products/show/range/
www.domain.com/shop/buy/products/show/range
redirect to:
www.domain.com/_shop/products.php?trg=products&range=range
and:
www.domain.com/shop/buy/products/show/range/color
www.domain.com/shop/buy/products/show/range/color/
redirect to:
www.domain.com/_shop/products.php?trg=products&range=range&color=color
one Another way is you Can Use Only This code
RewriteRule ^shop/buy/(.*?)/show/(.*)$ _shop/products.php?trg=$1&range=$2 [L,QSA]
after That split range in php By
list($range,$coler)=explode("/",$_GET['range']);
It is work too.

RewriteCond appending querystring

I am using .htaccess file to redirect my archived old aspx urls to my new php site. Now the requirement is as follows:
example.com/showad.aspx?adid=1234 should be example.com/ad/getad/1234
I googled the matter and got the following htaccess rule:
RewriteCond %{QUERY_STRING} ^adid=(\d+)$
RewriteRule ^showad\.aspx$ /ad/getad/%1
The rule works except that the resulting url is example.com/ad/getad/1234?adid=1234
I searched again and learned that to remove the (adid=1234) at the end of the output url you should place a question mark ? at the end of the substitution parameter, so that the rule should be:
RewriteCond %{QUERY_STRING} ^adid=(\d+)$
RewriteRule ^showad\.aspx$ /ad/getad/%1?
But it doesn't work!
The resulting url is STILL having the original query string appended to it. I am using this site to test my htaccess rules: htaccess.madewithlove.be
Any clues why is this happening?
Try changing your rule to
RewriteRule ^showad\.aspx$ /ad/getad/%1? [R=302,L]
Your rule is fine - I just tested it in my own .htaccess. It's the htaccess tester website that's wrong :)

.htaccess: how to only rewrite .php URL?

My rewriterule with a condition is working fine as below:
http://www.sitename.com/index.php?n=text redirects to
http://www.sitename.com/pages/text
and the page renders properly, however, there is a problem that with the redirected URL the arguments are also added to the URL. So actually in address bar it looks like-
http://www.sitename.com/pages/text?n=text
Could anyone help me on this? The htaccess code is given below.
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php http://www.sitename.com/pages/%1 [r=301,nc]
You probably want to catch "index.php.*". Otherwise mod_rewrite only replaces the "index.php" part of the URL "index.php?n=text" with the new URL.
Guss,
From what you suggested, i reconstructed it as follows:
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php.* http://www.sitename.com/pages/%1 [r=301,nc]
This doesnt seem to be working either. Can you please elaborate on what you have said?
thank you
aditya
donĀ“t use the url in the rewrite rule, apache then sends a http 200 code and then the 301...
try sth. like this:
RewriteRule (index\.php)(?n=)(.*) /pages/$3 [r=301]

Resources