I try using mod_rewrite but it doesn't seem to work, probally a stupid error on my side.
when i go to index.php?page=home i want to make the url cleaner, I get no error but it also doesn't work
this is in my .htaccess:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^page/([A-Za-z0-9-]+)/?$ index.php?page=$1 [NC]
The rule you have in htaccess only changes new links to the old ones ie /page/something to /index.php?page=something . This doesnt handle your old links.If you don't want your old links to be accessible you can redirect them to the new url format (cleaner version) using RewriteRule.
Add the following lines above your existing rules but bellow RewriteEngine on
RewriteCond %{THE_REQUEST} /index.php\?page=([^\s]+) [NC]
RewriteRule ^ /page/%1? [L,R]
To fix the css issue see this post
Seo Friendly Url css img js not working
Related
I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.
i changed my cms and have now some old urls, that are still linked by other websites.
Now i have to redirect the old links to my new sites Start Page.
The old CMS was installed in a subfolder named "contentms". The new CMS is installed in the root-folder.
Old Urls look like: mywebsite.com/contentms/content.php?idcat=62
When i try to forward it using my rewrite rule:
RewriteEngine on
RewriteRule ^contentms/.*$ http://www.mywebsite.com/ [R=301,L]
It works, but there will appear:
http://www.mywebsite.com/?idcat=62
But i want to have only http://www.mywebsite.com, without the "?idcat=62" added to the external url.
Maybe you have a solution for me.
Thanks!
Give this a try and see how it works for you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/contentms
RewriteRule ^(.*) http://www.mywebsite.com/? [R=301,L]
You can do this in the same line.
RewriteEngine on
RewriteRule ^contentms/.*$ http://www.mywebsite.com/? [R=301,L]
I have some old SWF that has fixed links inside, so I need to rewrite all the links to redirect to new site I build on Wordpress.
I actually need to redirect all links that have FID=<123> to /me_<123> (that's the permalink I created for all pages I need to redirect)
For example:
http://kavor.org.il/KavOr07/Templates/showpage.asp?DBID=1&LNGID=2&TMID=843&FID=1997
Needs to go to http://kavor.org.il/me_1997
I tried to put this in .htaccess:
^FID=([^/.]+)/?$ /me_$1 [R]
but it has no effect and also for some reason slows down the entire site loading.
I know that Wordpress has its own rewrites so maybe it's interefering.
I would be glad for some help with that I got pretty lost in all the mod_rewrite rules...
To match query string you will need a RewriteCond with %{QUERY_STRING}
Try this rule:
RewriteCond %{QUERY_STRING} (^|&)FID=([^&]+) [NC]
RewriteRule ^KavOr07/?$ /me_%1? [L,R=302,NC]
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.