Using WamServer , I used Generate It! to over write a URL like
The original URL:
http://localhost/CMS/foo.php?id=2&slug=eyeglasses
The rewritten URL:
http://localhost/2/eyeglasses.php
The rule which site gave me is
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /CMS/foo.php?id=$1&slug=$2 [L]
but this is still returning original URL
http://localhost/CMS/foo.php?id=2&slug=eyeglasses
Can you please let me know what I am doing wrong?
Update
To be more clarify! I have an index.php which contains a link like
echo ''.$theName.'';
now we user landed in foo.php the URL looks like
http://localhost/CMS/foo.php?id=1&slug=eyeglassess
correct? what I would like to do is displaying the
http://localhost/2/eyeglasses.php
instead of original URL
You need a redirect rule to redirect old URL to pretty URL. This code should be in /CMS/.htaccess
RewriteEngine On
RewriteBase /CMS/
# redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /CMS/+foo\.php\?id=([^\s&]+)&slug=([^\s&]+) [NC]
RewriteRule ^ %1/%2.php? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)\.php$ foo.php?id=$1&slug=$2 [L,NC,QSA]
However it is better to change your link code to new pretty link scheme.
Related
I am having some trouble rewriting and redirecting my URLs. This is what I am trying to do:
Currently I have this URL: domain.com/server.php?id=$1
I have that URL above which rewrites to: domain.com/details/$1
The problem I am having is when you go to: domain.com/server.php?id=$1 it doesn't redirect to my SEO friendly URL.
Does anyone know why?
This is my current rewrite for the url:
RewriteRule ^details/(.*)/ server?id=$1 [L]
Use this:
RewriteEngine On
RewriteRule ^details/([^/]*)$ /server.php?id=$1 [L]
It will give you the URL: domain.com/details/$1. Just make sure you clear your cache before testing it.
This is my what I found to work for me:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+server\.php\?id=([^\s&]+) [NC]
RewriteRule ^ details/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^details/([^/.]+)/?$ server.php?id=$1 [L,QSA,NC]
My question is how to get the URL in the RewriteRule to display when I click the link (domain.com/?brandID=1&name=Beretta).
My tests make me believe it might have something to do with index.php, but maybe not. My script is triggered by the GET variable being set, and queries the DB and includes the page that displays the results (and the "ugly" url).
Here's the .htaccess code:
# Rewrite for ?brandID=1&name=Name
RewriteRule ^pistol-brand/([0-9]+)/([0-9a-zA-Z_-]+) ?brandID=$1&name=$2 [NC,L]
I want to replace domain.com/?brandID=1&name=Beretta with domain.com/pistol-brand/1/beretta
The code above displays the "ugly" url, but when I change the url to what the RewriteRule states (domain.com/pistol-brand/1/beretta), the page works.
How do I get the "new" url to display when the link is clicked?
Thanks for any assistance.
You need one more to redirect the ugly url to sef format
##1)Redirect /?brandID=foo&name=bar to //pistol-brand/foo/bar##
RewriteCond %{THE_REQUEST} /\?brandID=([^&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /pistol-brand/%1/%2? [L,R]
##2)Rewrite /pistol-brand/foo/bar to /?brandID=foo&name=bar##
RewriteRule ^pistol-brand/([0-9]+)/([0-9a-zA-Z_-]+) ?brandID=$1&name=$2 [NC,L]
Sometimes I find that URLs on my website show up with mywebsite_smf_smf1_session=.
For example:
http://mywebsite.com/index.php?mywebsite_smf_smf1_session=6goevo28vf18ubqgb5uh6r08b2&topic=332242.msg916981
http://mywebsite.com/index.php?mywebsite_smf_smf1_session=6gqvo88lu5fnl1qmj7temt0113&topic=316196.msg931242
The part that contains mywebsite_smf_smf1_session=### shouldn't be in the URL, but the link still redirects to the correct page.
I would like to know how I can use mod_rewrite in .htaccess so that when someone clicks a URL containing mywebsite_smf_smf1_session=###&, it automatically reverts to the version not containing it:
Clicking:
http://
mywebsite.com/index.php?mywebsite_smf_smf1_session=6gqvo88lu5fnl1qmj7temt0113&topic=316196.msg931242
Becomes:
http:// mywebsite.com/index.php?topic=316196.msg931242
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mywebsite_smf_smf1_session=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^index\.php$ %{REQUEST_URI}?%1 [NE,L,NC,R=302]
I have a problem (it's probably a simple one but I've never had the need to write regex)
A SEO specialist told me to make pretty URLs so I did with the .htaccess file the CMS provides.
But now he requires me to redirect the old URLs to new ones.
This doesn't work
RewriteRule ^index.php?page=kontakt$ /kontakt.html [R=301,L]
and also this (wich was supposed to redirect to the main page from the index.php file)
RewriteRule ^index.php$ / [R=301,L]
has resulted in sitename.com/?page=kontakt, so now I also have to redirect this.
How do I fix this?
RewriteRule only matches the base URL without the query string. You need an additional RewriteCond for it to work.
RewriteCond %{QUERY_STRING} ^page=kontakt$
RewriteRule ^index.php$ /kontakt.html [R=301,L]
EDIT:
Apparently query string gets preserved in this case, so you're probably getting /kontakt.html?page=kontakt
To discard original query string you need to put ? after URL.
RewriteRule ^index.php$ /kontakt.html? [R=301,L]
understand how to make e.g. blog.php rewrite to just blog.
But I can't understand what I need to do when rewriting a dynamic URL.
The URL normally looks like so:
<a href='newspost.php?id=$postID&$blogtitleURL'></a>
This works fine as it should, but i want it to look more friendly.
E.g. newspost/5/Blog_title.
Is this possible? Also how if it is, do I go about making a variable using $_GET?
I normally use this for the rewrite:
RewriteRule ^apage apage.php [L]
What should I be doing for the URL provided?
Thanks!
This should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /newspost\.php\?id=(.*)&title=(.*)\ HTTP
RewriteRule ^ /newspost/%2/%3\? [R=302,L]
RewriteRule ^newspost/(.*)/(.*)$ /newspost.php?id=$1&title=$2 [L]