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]
Related
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.
I've made some search about .htaccess Rewrite function, but I couldn't find any example with an html reference.
I'm trying to redirect from my example.com/products/category/ link to another page.
Here's the link what I'd like the .htaccess file to redirect:
<a href="example.com/products/category/?category=bathroom">
I'd like to achieve this style:
example.com/products/category/bathroom/
Here's my .htaccess ReWriteRule:
RewriteRule ^products/([A-Za-z0-9-]+)/?$ /products/category.php/?category=$1 [L] # Process category
Note that I'm using two different php pages for both. page-category.php and category.php
I've places it after
RewriteEngine On
RewriteBase /
but still it doesn't work.. The only time something happened when I tried some combinations, it threw back an internal server error. Is there a problem with my RegEx or am I not doing the linking right ?
EDIT: I'm using wordpress, I forgot to mention that, sorry.
You need to use Redirect 301
RedirectMatch 301 ^example.com/products/category/?category= /example/home/page-category.php
Or you can use mod_rewrite instead:
RewriteEngine On
RewriteRule ^example/products/category/?category= /example/home/page-category.php [L,R=301]
same you can do for another file.
as i understood from your question you need something like this in category directory .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) category.php?category=$1 [QSA]
Now anything that is not a directory or a file inside category directory will be rewriten like /category/bathroom will be rewriten to (without redirection) /category.php?category=bathroom
I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]
This rewrite methode works, but its not Forcing urls to rewrite/redirect to the new urls.
I use this:
RewriteEngine on
RewriteRule ^page/([^/\.]+)$ search.php?q=$1
i can access rewrited urls (project/page/etc..) , but old urls/links (search.php?q=etc) still accessible without redirect.
note: i use $_SERVER variants to creating urls, and on localhost.
You have to rewrite project/page to search.php? in order to hide the ugly urls.
And redirect search.php? to project/page in order to make the canonical urls the only way to access that resource.
In your code there is no mention of the redirect, you're just rewriting.
Think of it this way
rewrite hides the ugly urls behind canonical urls (it hides them, it doesn't eliminate them);
redirect responds to the browser with a message like "search.php? has moved to project/page, try that link instead" and the browser follows the new link;
To redirect so called ugly URLs to SEO friendly URL you would need another Rewrite rule. Have your .htaccess code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^page/([^/\.]+) search.php?q=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?q=([^\s]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
Once you have verified that it's working fine change R=302 to R=301.
You'd need a [R=301] on the rewrite rule to turn it into a client-side redirect. Otherwise it's purely an internal rewrite and the client will never see the url getting changed.
e.g.
RewriteRule ^page/([^/\.]+)$ search.php?q=$1 [R=301,L]
With a bit of help from people here at Stackoverflow I've managed to put together a .htaccess file that permits 'pretty URLs'. This is great if a user types the 'pretty URL' directly into the address bar as the conversion works exactly as I would like it to do, but if a user clicks a link within my site that generates a dynamic link, the 'ugly URL' remains and the conversion doesn't take place. Is there something I need to add to the .htaccess file to get this to work, or do I need to code up some PHP to force the conversion for links?
My .htaccess file is set-up as follows:
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^mysite\.com$
RewriteRule ^(.*) http://mysite.com/$1 [R=301,L]
RewriteRule ^episode/(0|[1-9]\d{0,2})$ /episode.php?episode=$1 [L,QSA]
(Converts http://mysite.com/episode.php?episode=31 to http://mysite.com/episode/31.)
Just append this rule in the end to force pretty URL in browser:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episode\.php\?episode=(\d+) [NC]
RewriteRule ^ episode/%1? [R=302,L]
Once working change R=302 to R=301.