Rewriting URL using HTACCESS for category name - .htaccess

I want to rewrite the URL using htaccess... for converting below URL
http://www.example.com/category.php?id=xyz
TO
http://www.example.com/xyz
I don't want to write category in url, I am using below code but no use
RewriteRule ^(.*)/ category.php?id=$1
RewriteRule ^(.*) category.php?id=$1

You need to use a RewriteCond to manipulate query strings.
RewriteEngine on
RewriteCond %{THE_REQUEST} /category\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [NE,L,R]

Related

How redirect query search result from domain which contains a folder?

I have this code in my .htacces file:
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ job\?search_keywords=%1 [NC,R,L]
And it works well for: example.com/?s=blabla
Now I would like to add that if the url contains /en/:
https://example.com/en/?s=blabla
change for https://example.com/en/work/?search_keywords=blabla
Somone could help me with it? Because my trying gives me ERROR 500.
You just need another rule to handle new URI pattern:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^$ /job?search_keywords=%1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^en/?$ /en/work/?search_keywords=%1 [R=301,L]
Note that \\? in your RewriteCond is redundant as QUERY_STRING variable doesn't contain ?

how to redirect rewritten query-string URL

I am using following rules
RewriteRule ^([a-z0-9\-]+)/?$ category.php?cat=$1 [NC,L]
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ category.php?cat=$1&subcat=$2 [NC,L]
To rewrite following URL
mynews/category.php?cat=News
mynews/category.php?cat=News&subcat=9
to
mynews/News/
mynews/News/9
It is being rewritten only. But how to redirect automatically these querystring URL to rewritten URL ?
I have used following rules as suggested in answer, but doesn't work
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^&]+)&subcat=([0-9]+)
RewriteRule ^category\.php$ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^\ &]+)
RewriteRule ^category\.php$ /%1/? [L,R]
RewriteRule ^([a-z0-9\-]+)/?$ category.php?cat=$1 [NC,L]
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ category.php?cat=$1&subcat=$2 [NC,L]
You'll need to add a few rules to do external redirects. These redirect rules will need to be placed before the rewrite rules that you already have. So they'll look something like this:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^&]+)&subcat=([0-9]+)
RewriteRule ^category\.php$ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category\.php\?cat=([^\ &]+)
RewriteRule ^category\.php$ /%1/? [L,R]
These will see requests that are /category?cat=<something> and externally redirect the browser to the nicer looking URLs. The reason we need to match against %{THE_REQUEST} is so that we make sure there's no chance the rewrite rules that you already have interfere with the redirect rules because the rewrite engine will loop through the rules.

match query string variables with mod_rewrite?

The site I am trying to work on is http://northidahocvma.org/2GD/index.php?page=1
So suppose I have URLs with query string parameters like these:
/index.php?page=1
Using mod_rewrite, how can I redirect them to URLs like these?
/page/1 or /1/
I was given this code but still cant quite figure it out
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=302,L]
RewriteRule ^page/([^/.]+)/?$ /index.php?page=$1 [L,QSA,NC]
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=302,L]
RewriteRule ^page/([^/.]+)/?$ /index.php?page=$1 [L,QSA,NC]

.htaccess mod_rewrite cannot remove page name

Having trouble figuring out the mod rewrite for .htaccess I want the url http://www.example.com/archive.php?title=about_me which is a dynamic url to be rewritten to http://www.example.com/about_me. I am using php and here is my current .htaccess code, however it only rewrites to http://www.example.com/archive/about_me want the archive to be removed.
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /archive/%1? [L,R=301]
RewriteRule ^/?archive/(.*)$ /archive?title=$1 [L]
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
I did get it to rewrite correctly with this code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteRule ^/(.*)$ /archive?title=$1 [L]
However it then returns a page cannot be found error
I you want the /archive/ to be removed, you'll have to ensure that any URI that's in the form of /something must absolutely be routed to the archive.php script. Because there's simply no way to tell whether /my_blog is actually a request for /my_blog or whether it needs to be sent to the archive.php script with "my_blog" as the value of title in the query string. The best you can do is check that it's not a request for an existing resource via the -f and -d conditions:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
# no /archive/ ^
# condition checks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /archive?title=$1 [L]
Something like this should do the trick:
RewriteCond %{QUERY_STRING} ^title=(.*)
RewriteRule ^archive.php /%1?
RewriteRule ^(.*)$ /archive?title=$1 [L]
EDIT: Added the final RewriteRule as noted in my comments on the original question. Per the comment I believe you are trying to do the following two things:
Redirect any user-entered "real" URLs to the "friendly" URL: http://www.example.com/archive.php?title=about_me to http://www.example.com/about_me as stated in the question.
Rewrite the "friendly" URL to the "real" URL: http://www.example.com/about_me to http://www.example.com/archive.php?title=about_me, which was not clear as stated.

Pretty URL getting redirected to query part

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]

Resources