Pretty URL getting redirected to query part - .htaccess

I am using .htaccess for URL rewriting. I have used code like:
RewriteRule ^([a-zA-Z0-9-_]+)$ index.php?page=d/ctg/item&itmctgals=$1 [QSA]
and URL is smstongue.com/friendship-sms (the alias URL) but it is showing URL with query part in URL i.e. smstongue.com/?page=d/ctg/browse&itmctgals=friendship-sms
But I want to show URL like smstongue.com/friendship-sms in browser URL bar.
How to do this?

If I understand you correctly,
do this:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} itmctgals=([\w\d-]+) [Nc]
RewriteRule ^ %1/? [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w\d-]+)$ index.php?page=d/ctg/item&itmctgals=$1 [L,QSA]

Related

Redirect urls with question mark and other parameters in htaccess

I want to redirect pages like:
/category-name/post-name.html?id=1234
To:
/category-name/1234-post-name.html
How can do this using htaccess?
What I have tried:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^([^/]+)/([^.]+)\.html$ /$1/%1-$2\.html [L,R=301]
But it is a continuous redirect.
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([\w-]+)/([\w-]+)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%3-%2? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^([\w-]+)/([^-]+)-([\w-]+)/?$ $1/$3?id=$2 [L,QSA]
Try this
Make sure the url is root url
Example:-
www.foo.com/category-name/post-name.html
It will only work if the project url is same as that of the above.
www.foo.com/blog/category-name/post-name.html
This won't work you need to update the RewriteBase url accordingly l.
This is the conditions
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule here
RewriteRule ^category-name/([0-9]+)-post-name$ /category-name/post-name.html?id=$1 [L]
This should work..

How to rewrite paged URL's?

I have changed my web page url from localhost/products.php to localhost/products using the code below.
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
But when I navigate to next page, address bar shows something like:
localhost/products?page=2 which I want to appear as localhost/products/page2
Tried this RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L] but this did not work.
My full code for rewriting page url is:
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L]
Here how can I change the URL for next pages? I mean from localhost/products?page=2 to localhost/products/page2 and so on.
You can use the following rule :
RewriteCond %{THE_REQUEST} /products/?(?:\.php)?\?page=([^\s&]+) [NC]
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/([0-9]+)/?$ /products.php?page=$1 [L,NC]
This will convert the urls
/products.php?page=number
/products/?page=number
to
/products/number

Block or redirect an url with some ampersands

Need to redirect or block this url.
home/?option=com_content&task=view&id=49&Itemid=78
None solutions worked so far. Tried also this
RewriteRule ^home/?option=com_content&task=view&id=49&Itemid=78$ - [F]
Any suggestions?
You can use the following Rule to block the Url :
RewriteEngine On
RewriteCond %{THE_REQUEST} /home/\?option=com_content&task=view&id=49&Itemid=78 [NC]
RewriteRule ^ - [F,L]
Or try this, if you want to redirect the url to hompage :
RewriteEngine On
RewriteCond %{THE_REQUEST} /home/\?option=com_content&task=view&id=49&Itemid=78 [NC]
RewriteRule ^ /? [R,L]

htaccess rewriting specific folder/path to subdomain

I'm trying to setup a redirect for a site.
I want rewrite file requests from a folder to a subdomain url.
At the moment, I have this in my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} =GET
RewriteCond %{HTTP_HOST} !^example.com
RewriteCond %{HTTP_HOST} ^g.example.com
RewriteRule ^(uploads/(.*)+ uploads/%1%{REQUEST_URI} [L]
The idea is that when a visitor requests:
http://g.example.com/uploads/somefile.png
they are served:
http://example.com/uploads/somefile.png
So it's working as expected. However the problem is that:
http://b.example.com/uploads/somefile.png
also works.
I only want http://g.example.com/ to be rewritten. http://b.example.com/ should 404.
This should work:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^g.example.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.(JPG|GIF|PNG) [NC]
RewriteRule ^ - [R=404,L]
RewriteCond %{HTTP_HOST} ^g.example.com$ [NC]
RewriteCond %{THE_REQUEST} !^GET [NC]
RewriteRule ^ - [R=404,L]
The first rule will make sure that any URL ending with JPG, GIF, PNG that does not come from g.example.com send a 404 not found request to the user.
The second rule makes sure that g.example.com are only accessed using GET requests.

.htaccess RewriteRule without URL change in browser

I have .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^somedomain.com [NC]
RewriteRule ^(.*)$ http://www.somedomain.com/$1 [R=301,NC]
RewriteRule ^r/(.*)$ index.php?rid=$1 [NC]
But last condition performs redirect in browser when I request URL like www.somedomain.com/r/123 and show me URL like www.somedomain.com/index.php?rid=123. But I need call this script without URL change.
What's wrong?
You used the R=301 Try to use L just like following:
RewriteCond %{HTTP_HOST} ^somedomain.com [L]
RewriteRule ^(.*)$ http://www.somedomain.com/$1 [L]
RewriteRule ^r/(.*)$ index.php?rid=$1 [L]

Resources