Create a pretty url without touching the file - .htaccess

Is it possible to create a pretty url without having to touch the file itself? For example:
http://sawebdev01/company/news-events/press-releases/press-release/?pr=168#.Uajh_kCyBSK
And convert it to:
http://sawebdev01/company/news-events/press-releases/pr/168
I tried this, and it does not seem to work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} /company/news-events/press-releases/press-release/?pr=([^&]+) [NC]
RewriteRule .* /company/news-events/press-releases/press-release/pr/%1? [R=301,L]

Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s(/company/news-events/press-releases)/press-release/\?pr=([^&]+) [NC]
RewriteRule ^ %1/pr/%1? [R=302,L]
RewriteRule ^(company/news-events/press-releases)/pr/([^/]+)/?$ /$1/press-release?pr=$2 [L,NC,QSA]

Related

Configure correct htaccess rules

i use htaccess
Options +FollowSymLinks
RewriteEngine on
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule detalhe/(.*) detalhe.php?id=$1
i want redirect:
https://example.com/detalhe.php?ID=31&URL=lorem-to-lorem
to:
https://example.com/31/lorem-to-lorem.html
any ideias to help?
You may use an additional rule to handle additional URI segment:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/detalhe\.php\?ID=(\d+)&URL=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(\d+)/(.+)$ detalhe.php?ID=$1&URL=$2 [L,QSA,NC]
RewriteRule ^detalhe/(.*) detalhe.php?id=$1 [L,QSA,NC]

url rewriting with QUERY_STRING

I would like to rewrite http://example.com/test/index.php?id=123
to http://example.com/test/item/123/ and http://example.com/test/index.php?id=123 redirect to the human friendly url.
I don't know what is wrong in my htaccess :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /test/
RewriteCond %{QUERY_STRING} ^id=([a-z0-9]+)$ [NC]
RewriteRule ^/item/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^item/([a-z0-9]+)/$ index.php?id=$1 [L]
You can use the following RewriteRule in your /test/.htaccess file
Options +FollowSymLinks -MultiViews
RewriteEngine on
#redirect old url to the new one
RewriteCond %{THE_REQUEST} /test/index\.php\?id=([^\s]+) [NC]
RewriteRule ^ /test/item/%1? [L,R=301]
#internally map new url to the old one
RewriteRule ^item/(.+)/?$ /test/index.php?id=$1 [L,NC]

Again stuck with HTACCESS

What am I doing wrong here? It does not work for me :(
Options +FollowSymLinks -MultiViews
RewriteEngine on
# use appropriate rewrite base
RewriteBase /stage/customer/be/
RewriteCond %{REQUEST_METHOD} !^(TRACE|TRACK|GET|POST|HEAD)$
RewriteRule ^ - [F]
RewriteCond %{THE_REQUEST} /index\.php\?action=([\w-]+)&template_id=([\w-]+)&feedbackTypeID=([\w-]+) [NC]
RewriteRule ^ %1/%2/%3? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?action=$1&template_id=$2&feedbackTypeID=$3 [L,QSA]
This is a continutation of this thread again a new .htaccess issue
I have this code in the htaccess one folder above from where the call is being made. Not sure if this would affect the "be" folder as well.
Options +FollowSymLinks -MultiViews
RewriteEngine on
# use appropriate rewrite base
RewriteBase /stage/customer/
RewriteCond %{REQUEST_METHOD} !^(TRACE|TRACK|GET|POST|HEAD)$
RewriteRule ^ - [F]
RewriteCond %{THE_REQUEST} /index\.php\?id=([\w-]+)\s [NC]
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?id=$1 [L,QSA]

redirection and mapping for same page using htaccess

redirection and mapping for same page using htaccess not working
Ex . www.xyz.com?view=aa&param=bb -- redirect it to www.xyz.com/aa/bb
Then map www.xyz.com/aa/bb to
www.xyz.com?view=aa&param=bb
below is the rules.
redirection
RewriteCond %{QUERY_STRING} ^view=([^&]*)&p=([0-9]+)&/(.*)$
RewriteRule ^insights/(.+?)\.html$ insights/$1/%1/%2-%3.html? [R=301,L]
mapping
RewriteRule ^insights/(.*)/(.*)/([0-9]+)-(.*)\.html$ /insights/$1.html?view=$2&p=$3&/$4 [L,QSA,NC]
This should work to what you're trying to do:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /insights/anything.html?view=anything&p=anything&/anything
# To /insights/anything/anything-anything.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/([^.]+)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3/%4-%5.html? [R=302,L]
# Internally forward /insights/anything/anything-anything.html
# To /insights/anything.html?view=anything&p=anything&/anything
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^-]+)-([^.]+).html$ /$1/$2.html?view=$3&p=$4&/$5 [L]
Change R=302 to R=301 only after you have confirmed the rule is working to avoid your browser from caching wrong redirects.
Keep these 2 rules at top of your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?view=([^&]+)&param=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /?view=$1&param=$2 [L,QSA]

Make url-rewrite in .htaccess file to work both ways

I have the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlPram=$3 [L,NC]
</IfModule>
when my url format is something like:
mysite.com/project/1/test/vw
everything works fine. But I would also like when the url is typed in the other format, to be rewritten to the first format.
i.e. When I type the url like this:
mysite.com/project.php?id=1&pName=test&urlParam=vw
I wants it to be turned into:
mysite.com/project/1/test/vw
Do I have to write another rule? or is it accomplished in some other way?
The current rewrite rule would not handle both the cases. Add the below rewrite rule as well.
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlParam=$3&no-redir [L,NC,QSA]
RewriteCond %{QUERY_STRING} !no-redir
RewriteCond %{QUERY_STRING} (^|&)id=([^&]+)&pName=([^&]+)&urlParam=([^&]+)
RewriteRule ^project\.php$ project/%2/%3/%4? [R=301,L,NC]
You need one more external redirect rule. Use this code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} /project\.php\?id=([^&]+)&pName=([^&]+)&urlPram=([^&\s]+) [NC]
RewriteRule ^ project/%1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlPram=$3 [L,NC,QSA]
</IfModule>

Resources