I need rewrite this url http://adresa.com/article.php?act=view&id=1113 to http://adresa.com/view/1113
I make it with mod_rewrite in .htaccess but it doesn't work.
RewriteBase /
RewriteCond %{QUERY_STRING} ^act=([^&]+)$
RewriteRule ^article\.php$ %1.html? [R=301,L,NE]
RewriteRule ^([^/]+)\.html article.php?rw=1&act=$1 [L,QSA]
RewriteRule ^([^/]+)/{1}([^/.]+)\.html article.php?rw=1&act=$1&id=$2 [L,QSA]
Can somebody help me please?
Here's the solution (with proper line-separation to avoid confusion (RewriteCond is valid for only one following RewriteRule, so put a blank line after to avoid confusion)):
RewriteBase /
RewriteRule /view/([0-9+])$ /article.php?act=view&id=$1 [QSA,L]
RewriteCond %{QUERY_STRING} ^act=([^&]+)$
RewriteRule ^article\.php$ %1.html? [R=301,L,NE]
RewriteRule ^([^/]+)\.html article.php?rw=1&act=$1 [L,QSA]
RewriteRule ^([^/]+)/{1}([^/.]+)\.html article.php?rw=1&act=$1&id=$2 [L,QSA]
Related
I have a .htaccess file, but I'm not sure if it'll actually harm me instead of help me. How should I do this better to have search engines not penalize me with duplicate content?
I don't want them to rank login.php or /login, just /login/. And simiarly, if someone visits login.php or /login, I'd like them to be forced to /login/. How can I do this and optimize correctly?
RewriteEngine On
RewriteBase /
RewriteRule ^login/$ login.php [NC,L]
RewriteRule ^register/$ register.php [NC,L]
RewriteRule ^recover/$ recover.php [NC,L]
RewriteRule ^recover/([a-z0-9]+)/$ recover.php?code=$1 [NC,L]
RewriteRule ^game/([a-z0-9-]+)/$ game.php?game=$1 [NC,L]
You can have some new redirect rules to redirect to pretty URLs:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /(register|login|recover)(?:\.php)?[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s/+(game|recover)\.php\?code=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L,NE]
RewriteRule ^(register|login|recover)/$ $1.php [NC,L]
RewriteRule ^(game|recover)/([a-z0-9]+)/$ $1.php?code=$2 [NC,L,QSA]
I try to rewrite my URLs from domain.tld/?country=XX&admin1=YY&city=ZZ to domain.tld/XX/YY/ZZ
I have tried
RewriteEngine on
RewriteRule ^([A-Za-z]{2,2})/(.*)/(.*)$ /?country=$1&admin1=$2&city=$3 [QSA]
Unfortunately it doesn't work. Have I missed something?
You can have these rules in your root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /\?country=([^\s&]+)&admin1=([^\s&]+)&city=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L,NE]
RewriteRule ^(\w+)/(\w+)/(\w+)/?$ /?country=$1&admin1=$2&city=$3 [QSA,L]
I need to rewrite my url from:
http://***.com/index.php?cat=VAR&page=1
to:
http://***.com/VAR/1
With 301 redirection.
I got this so far:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index.php\?
RewriteCond %{QUERY_STRING} ^cat=(.*)\&page=(.*)
RewriteRule . /%1/%2 [R=301]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(files|admin)/
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L]
But the first 3 rules doesn't seems to work at all. (I'm a beginner in htaccess)
How can i fix this problem? Thanks!
EDIT :
Thanks to Jassie, the solution:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index.php\?
RewriteCond %{QUERY_STRING} ^cat=(.*)\&page=(.*)
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(files|admin)/
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L]
change it to RewriteRule ^(.)/(.)$ /index.php?cat=$1&page=$2 [L,QSA] and try
I am attempting to get the following results with an .htaccess file.
URL: Behavior(rewrite to):
example.com/x/ view-test.php?page=x
example.com/x/y view-test.php?page=y&parent=x
example.com/x/y/z view-test.php?page=z&parent=y&grandparent=z
I have the following .htaccess rules, which also include some folders which needs to ignore this scheme:
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
#RewriteRule ([^/]+)$ /view-test.php?page=$1 [L]
however, whenever I uncomment the last two lines I get an internal server error. the parent/page and grandparent/parent/page structures work fine. How can I re-write this last rule (and the preceding rules if nessecary) to achieve the desired result for the first pattern (just a page)?
# remove slash at the end to prevent redirect loop
RewriteCond %{REQUEST_URI} ^/(OIE|admin|images|scripts|css|fonts|view-test\.php) [NC]
RewriteRule ^ - [L]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /OIE/view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /OIE/view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]+)/?$ /OIE/view-test.php?page=$1 [L]
Use this code instead:
RewriteCond %{REQUEST_URI} ^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ^ - [L,S=3]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ view-test.php?grandparent=$1&parent=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ view-test.php?&parent=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ view-test.php?page=$1 [L,QSA]
Problem was that you were missing trailing slash / in in your last RewriteRule condition.
Try this :
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ view-test.php?page=$1
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2&grandparent=$3
I wan't the server to always redirect my URL's to format as "http://www.domain.com", even if the user write just "domain.com".
I could find some examples of this on the web, but I already have some fixes in .htaccess file and I don't know, where to put what, so it does't clash with the previous code.
Here's my .htaccess file:
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404
RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3&detail4=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?detail1=$1 [QSA,L]
What should I put in, so it does the URL redirect to "www"?
And one last question, is it all I have to do, so search engines don't have problems with the URL's?
I usually do the contrary.
you probably want www.example.com to forward to example.com -- shorter URLs
are sexier.
no-www.org/faq.php?q=class_b
Thanks to HTML5 boilerplate, I usually add :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
If you want to do that in your .htaccess :
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3&detail4=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?detail1=$1 [QSA,L]
Else, try the inverse RewriteCond and associate RewriteRule
Your [^/\\.] pattern simplifies to [^/.] as the period does NOT need escaping.