Replacing Segment with mod rewrite in .htacces - .htaccess

I'm trying to change this URL:
domain.com/?ACT=52&id=28
to this one:
domain.com/site.html
I want to replace the last segment ?ACT=52&id=28 to site.html. There are no parameters to consider. The URL domain.com/?ACT=52&id=28 will always be the same.

Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ACT=52&id=28$
RewriteRule ^/?$ /site.html [L]
This makes it so when someone goes to domain.com/?ACT=52&id=28, they get served the content at domain.com/site.html, but if you to change the URL that's in the browser's URL address bar, you need to REDIRECT, not internally rewrite, so add the redirect flag in the square brackets: [L,R]

Related

How to use .htaccess to redirect while maintaining the original URL

I am trying to use the .htacces file to redirect any files or subfolders in a derectory to one file, while still maintaining the original URL input.
So if a user goes to:
https://example.com/folder1/folder2/folder3/
or
https://example.com/folder1/folder2/file.php
it would redirect them back to:
https://example.com/folder1/index.php
but the original URL input would not change.
You can use RewriteRule . In htaccess in the document root add the following rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder1/index\.php$ [NC]
RewriteRule ^folder1/.+$ /folder1/index.php [L]
This will redirect /folder1/foo/bar to /folder1/index.php without changing the url in browser.
The RewriteCond above makes sure you don't rewrite /folder1/index.php to itself (/folder1/index.php) otherwise the rule can cause an infinite loop error.
You can just make :
RedirectMatch 301 ^https://example.com/folder1/ https://example.com/folder1/index.php
This allows you to redirect from the first url in the pattern to the
second one

Redirect index.php with parameters to a folder and remove parameters using htaccess

I have searched but cannot find a specific answer for this exact redirect style...
I have this structure of URL with this specific parameter:
https://websitename.com/directory/index.php?option=com_virtuemart&view=cart
I want it redirected to:
https://websitename.com/shopping-cart/
Note that the above mentioned "directory" changes, but the index.php with the parameters stay the same. No matter what the directory is, I always want it to go to the same exact redirect.
I cannot seem to get the right redirect working in htaccess. Can anyone help?
You can use this redirect rule as your first rule in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?option=com_virtuemart&view=cart [NC]
RewriteRule ^ /shopping-cart/? [L,R=308]
# remaining rules go below this
You can use a set like this. It takes care on the param view=cart
RewriteCond %{QUERY_STRING} (^|&)view=cart
RewriteRule ^(.*)$ /shopping-cart/? [L,NC,R=301]
If you want to keep the querystring params, then change
/shopping-cart/?
to
/shopping-cart/
without questionmark

rewriting url to hide real page path

I recently with the help of SO solved my htaccess rewriterule issue:
Reuse subdomain in a rewrite rule
The result is that when somebody enters
whatever.example.com/anypage
in the adress bar, the htaccess automatically redirects to
whatever.example.com/somepath/whatever/anypage
What I wish to do is to find a way to just show whatever.example.com/anypage in the adress bar with the content of whatever.example.com/somepath/whatever/anypage displayed.
In the post I mentioned earlier, Jon Lin clearly mentions the following:
redirects always change what's in the browser's URL address bar
However I know some very frequent cases of url rewritting that would show in the adress bar let's say, for instance:
example.com/article-1-15
but actually showing the content of
example.com/somepath/somepage.php?article=1&otherparam=15
How could this apply to my case? I really wish to have a tiny url but it seems I missed something.
You may try something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://whatever.example.com/somepath/whatever/%1 [L]
It will map:
http://whatever.example.com/anypage
To a resource at:
http://whatever.example.com/somepath/whatever/anypage
showing always in the browser's address bar:
http://whatever.example.com/anypage
UPDATED
If whatever is dynamic and is the same in the substitution URI, here is another option:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ([^/]+)\.example\.com.*
RewriteCond %{REQUEST_URI} ^/([^/]+)$
RewriteRule .* http://%1.example.com/somepath/%1/%2 [L]
This will work as long as both "whatever" in the substitution path are the same. If they are not, the last "whatever" has to be hardcoded, like this:
RewriteRule .* http://%1.example.com/somepath/whatever/%2 [L]
There is no other way as the incoming URL doesn't have it.

restricting input of GET url on htaccess rewrite url

My original url is : www.site.com/report.cgi?d=2012-05
Requested URL: www.site.com/report-2012-05.cgi
My Htaccess Code:*
RewriteRule ^report([^/]*)\.cgi$ /report.php?d=$1 [L]
I want to restrict the request parameter to just XXXX-XX number format in GET url.
How can I do this ?
I didn't really understand your question, except you want to modify the URL format placing the parameter value in a different position.
The best way to do it is by capturing the query string like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} d=(.*)
The value inside the round brackets is the parameter value (2012-05), which can be back referenced with %1. For example:
RewriteRule .* report-%1.cgi [L]
Will rewrite the URL with /report-2012-05.cgi
Hope this helps.
I think you need to remove .cgi from your rewrite rule
For www.site.com/report-xxxx-xx
RewriteRule ^report-([^/]*)$ /report.cgi?d=$1 [L]<br>
For www.site.com/xxxx-xx
RewriteEngine On
RewriteRule ^([^/]*)$ /report.cgi?d=$1 [L]

htaccess URL Rewrite for products doesn't redirect me

There is plenty of information out there but nothing I've read on the interwebz has given me an answer as to why my htaccess is not working.
I cannot determine why my rule isn't rewriting the URL as I thought it would. I have the following url:
domain.com/Book/bookpage/index.php?bookID=123&bookName=foo_bar
I would like to change it so that when someone hits that URL, it shows like:
domain.com/Book/123/foo_bar
I started off trying to get it to work using just the Book ID and haven't even gotten that to work.
This is what I have thus far:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2
However, after placing that htaccess in the root of the site and going to the URL:
domain.com/Book/bookpage/index.php?bookID=123
The URL in the address bar remains the same.
try this:
RewriteEngine On
RewriteBase /
# this rewrite domain.com/Book/123 or domain.com/Book/123/
RewriteRule ^Book/([0-9]+)/?$ /Book/bookpage/index.php?bookID=$2 [L,NC,QSA]
# this rewrite domain.com/Book/123/title or domain.com/Book/123/title/
RewriteRule ^Book/([0-9]+)/([a-z0-9\-_]+)/?$ /Book/bookpage/index.php?bookID=$1&bookName=$2 [L,NC,QSA]
Try adding [L,R=301] at the end of the line:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2 [L,R=301]

Resources