Url rewrite with underline automaticly - apache

I'm trying to rewrite url automatically from
www.example.com/test.php?title=123jane
to
www.example.com/test_123jane
I'm using
RewriteRule ^test_([A-Za-z0-9-]+)/?$ test.php?title=$1 [NC,L]'
But this doesn't change it automatically.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /test\.php\?title=([a-z0-9]+) [NC]
RewriteRule ^test\.php$ /test_%1? [R=301,L,NC]
RewriteRule ^test_([a-z0-9]+)$ /test.php?title=$1 [L]
The above should work.

Related

301 redirect doesnot work in htaccess

I want to redirect from https://*****.com/lp/index.html
to https://*****.com/lp/
so I put these two line in .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
and now the whole redirect block in my htaccess file is like this:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
but the index.html redirect not working.
Does anyone know why? Thank you.
Use it like this:
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=301,NC,NE]
# remove /lp/index.html
RewriteCond %{THE_REQUEST} \s/+lp/index\.html [NC]
RewriteRule ^ /lp/ [L,R=301,NE]
Clear your browser cache and retest.
try this
http://www.rapidtables.com/web/tools/redirect-generator.htm
with Apache .htaccess redirect option

url rewrite in php for change posts url

I want change url:
http://example.com/posts.php?action=view&id="a number"
to
http://example.com/posts/"a number"
I write these into the .htaccess file but they don't work:
RewriteEngine On
RewriteRule ^posts/([0-9]+)/?$ posts.php?action=view&id=$1 [NC,L]
RewriteRule ^posts/?$ posts.php [NC,L]
It'll be like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\?action=view&id=(\d+) [NC]
RewriteRule ^posts\.php$ /%1/%2? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\ HTTP [NC]
RewriteRule ^posts\.php$ /%1/? [R=301,L,NC]
RewriteRule ^(posts)/(\d+)/?$ /$1.php?action=view&id=$2 [L]
RewriteRule ^(posts)/?$ /$1.php [L]
You can try below in your .htaccess file
.htaccess file code:
RewriteEngine On
RewriteRule ^posts/([0-9]+) /posts.php?action=view&id=$1 [QSA,L]
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite.
L means if the rule matches, don't process any more RewriteRules below this one.

Redirect index.php to root but not subfolders

I want to redirect www.domainname.com/index.php to www.domainname.com.
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
I used this code, but it redirects www.domainname.com/admin/index.php as well.
However, I don't want to redirect www.domainname.com/admin/index.php.
How can I achieve that?
You can use this rule:
RewriteCond %{THE_REQUEST} \s/+index\.php
RewriteRule ^index\.php$ / [R=301,L]

Apache .htaccess: How to remove question mark from URL if not `?id=(.*)`?

How to make .htaccess to remove question mark from URL if not ?id=(.*)?
# Rewrite for ?id=(.*)
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]
# It does not work out on this way
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !^id=.*
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]
This would be the right rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?#\ ]*)\?[^\ ]*\ HTTP/ [NC]
RewriteCond !{QUERY_STRING} id
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]
Update:
# Query rewrite exceptions
RewriteCond %{QUERY_STRING} !callback=.*
Does this work?
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ^(.*)$ $1?%1 [R=301,L]
Tip: during your testings, use 302 redirections instead of 301, as 301 redirections are stored by browsers. You can finally switch to classic 301 when you are done testing.

Redirect not working for complex rewrite rules (.htaccess)

I'm trying to make a redirect from a non-www version of the link to the www one. Its working fine for something like http://mywebsite.com but it fails for a request like http://mywebsite.com/artists/metallica/ or even a complex one. The whole .htaccess file is bellow. Any clues?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*).html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mywebsite.com/$1/ [L,R=301]
RewriteRule ^artists/([^/-]+)-p([^/]+)/$ /artists.php?l=$1&p=$2 [QSA,L]
RewriteRule ^artists/([^/]+)/$ /artists.php?l=$1 [QSA,L]
RewriteRule ^submit/$ /submit.php [QSA,L]
RewriteRule ^users/$ /users.php [QSA,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mywebsite.com/$1 [R=301,L]
Try this rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But make sure that you put this rule in front of those rules that just do an internal rewrite. Otherwise an already internally rewritten rule might get redirected externally.