Rewrite URL with 1 or several query in htaccess - .htaccess

I have a URL with query that one of it's query maybe is not set.
Target URL:
domain.com/category.php
Query of URL:
1. c = any character in UTF-8 [It is required]
2. page = number [It is not required and maybe not set]
Now, how change URL in .htaccess:
from domain.com/category.php?c=anyCharacter to domain.com/category/anyCharacter/
and from domain.com/category.php?c=anyCharacter&page=number to domain.com/category/anyCharacter/page:number/
UPDATE:
I found an answer but it is not complete.
RewriteRule ^category/(.*)/$ category.php?c=$1 [QSA,L]
This will change URL:
from domain.com/category/anyCharacter/ to domain.com/category.php?c=anyCharacter
and from domain.com/category/anyCharacter/?page=number to domain.com/category.php?c=anyCharacter&page=number
But it's not good answer because I don't want any user see Query in my URL.
Please do not write my answer again.

You will need 2 rewrite rules for this:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^category/([^/]+)/?$ category.php?c=$1 [QSA,L,NC]
RewriteRule ^category/([^/]+)/page:(\d+)/?$ category.php?c=$1&page=$2 [QSA,L,NC]

So you need to go from pretty URL to variables?
You could try something like this:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/\.]+)/?$ category.php?c=$1&page=$2 [QSA,L]
This will translate yourdomain.com/a/2 to yourdomain.com?c=a&page=2

Related

htaccess redirection for double slashes

I am having a problem with my site. Sometimes after the user log in is redirected to a page like this:
mydomain.com//somepage
Please notice the double slash in the URL, which takes to a not valid page so i want to modify my htaccess in order to make all pages with double slash to automatically redirect to an URL like this:
mydomain.com/folder/somepage
Please notice the word "folder" between slashes this time. An URL like this would always take to a valid page.
I made some rewrite rules for my htaccess but they dont work as expected:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
</IfModule>
Could you please give me a hint?
Thank you.
Try something like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
You need to match against the %{THE_REQUEST} variable because the URI gets normalized before it gets matched against the pattern of rewrite rules.

Not redirecting for clean URL

So I'm running into an issue here that I believe I have correct yet it's just not working for me here.
So simple enough I want me current URL which is:
tremorelights.com/chandelier-c-18.html?page=2&osCsid=vtemi4nqlioftbteam6nap0s77
To look like this URL:
tremorelights.com/chandelier/2/
So I'm trying to redirect it to the clean URL and this is what I have now.
RewriteEngine On
RewriteBase /
RewriteRule ^chandelier-c-18.html?page=$1 chandeliers/([0-9]+) [QSA,L,R=301]
Yet this is not working at all. When I manually input the clean URL it does not give me a 404 error but it does not take me to any other page like it should.
You can match against the query string in a rewrite rule, what you want to match against is the %{THE_REQUEST} variable here.
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+chandelier-c-18.html\?page=([0-9]+)
RewriteRule ^ chandeliers/%1? [QSA,L,R=301]
then maybe:
RewriteRule ^chandeliers/([0-9]+) chandelier-c-18.html?page=$1 [L]
Change your rule to this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^chandeliers/([0-9]+)/?$ chandelier-c-18.html?page=$1&osCsid=vtemi4nqlioftbteam6nap0s77 [QSA,L,NC]

Apache RewriteRule not rewriting as expected

Can you any one see anything wrong with the following apache rewrite rule:
This is in my .htaccess file inside a folder called "text" a subdirectory of localhost/lombardpress I have the following rule
Options +FollowSymlinks
RewriteEngine on
RewriteRule ([^/]+) /textdisplay.php?fs=$1 [NC]
I was expecting this input:
http://localhost/lombardpress-dev/text/lectio1
to rewrite to this:
http://localhost/lombardpress-dev/text/textdisplay?fs=lectio1
But instead I get a 404 error.
The requested URL /textdisplay.php was not found on this server.
It looks to me like the RewriteRule has re-written the address but not as I intended - so there must be something wrong with my regular expression.
Let me know if I can provide further information.
Try this
Options +FollowSymlinks
RewriteEngine on
RewriteBase /lombardpress-dev/text/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+) textdisplay.php?fs=$1 [NC]
With that rewrite cond you wont redirect textdisplay.php to itself again.
The problem is that [^/]+ matches all but / so it matches even textdisplay.php
Remove leading slash in target URL:
Try this code:
Options +FollowSymlinks
RewriteEngine on
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ textdisplay.php?fs=$1 [NC,L,QSA]
Reference: Apache mod_rewrite Introduction
Get rid of the / in front of the target:
RewriteRule ([^/.]+) textdisplay.php?fs=$1 [NC]
# no slash here ----^

Url changed but page remains same

I want to change my url
http://www.abc.com/search_result.php?id=110
to
http://www.abc.com/110
Here is the code which i am using.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search_result\.php\?id=([^\s]+) [NC]
RewriteRule ^ http://abc.com/%1? [R=301,L]
But the problem is, url changed to http://www.abc.com/110 , but page remain same.
Please anybody help !
One thing more i want to ask . Suppose i want to add more parameter in original url:
Say,
http://www.abc.com/search_result.php?id=110&name=amit
then what i should do to get the result.
http://www.abc.com/i-am-amit
Thanks !
You need an internal rewrite rule also for showing actual content from search_result.php"
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search_result\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ http://abc.com/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ search_result.php?id=$1 [NC,L,QSA]
Also for http://www.abc.com/search_result.php?id=110&name=amit how do you want pretty URL to be? Keep in mind that you will need both id & name in pretty URL such as:
http://www.abc.com/110/amit
Is that how you want?

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources