.htaccess Rewrite Filename as Page - .htaccess

I'm trying to have my .htaccess file rewrite a URL with this ending:
/Management_Agreement_fillable.pdf
as:
/?page=viewer&viewer=G&fileID=1
However, when I try the following:
RewriteCond ^/Management_Agreement_fillable.pdf$
RewriteRule (.*) /?page=viewer&viewer=G&fileID=1 [R=301,L]
It doesn't work. What should I be doing?

You don't need the condition:
RewriteRule ^Management_Agreement_fillable\.pdf$ /?page=viewer&viewer=G&fileID=1 [R=301,L]
If you don't want the URL in the address bar to change, remove the R=301, from the square brackets.

Related

with .htaccess how do I remove slashes from some point?

I am trying to make a user system, where the URL is like:
test/user/[name]
It all works, but if you try:
test/user/name/something
It doesn't work. Is there any way to remove the slashes from that certain point, so that:
test/user/name/something
will go to:
test/user/name
How do I edit this, so it works as mentioned above:
RewriteEngine on
RewriteCond %{REQUEST_URI} user/(.*)
RewriteRule user/(.*) something/test/user.php?user=$1
You can place this rule as your first rule in your root /test/.htaccess:
RewriteEngine on
RewriteBase /test/
RewriteRule ^(user/\w+)/.+$ /$1 [L,NC,R]
RewriteRule ^user/(.+)$ user.php?user=$1 [L,QSA]

Remove file name in a URL

How can i remove a file name in url with mod_rewrite.
ex: searchpage-some+search.html in to some+search.html
This is my .htaccess code code.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /searchpage-search-%2.html? [R,L]
#Internal rewrite
RewriteRule searchpage-search-(.*)\.html$ searchpage.php?search=$1 [L]
This is for a search form that uses $_GET request. This works well only thing is that i want to remove the file name. I would really appreciated if anyone can help out.
In your example you have searchpage-some+search.html but in your htaccess you are actually rewriting it to searchpage-search-some+search.html.
But to remove the searchpage-search from links, just remove searchpage-search- from the RewriteRule. Your htaccess would be something like
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /%2.html? [R,L]
#Internal rewrite
RewriteRule (.*)\.html$ searchpage.php?search=$1 [L]

.htaccess redirect, change GET variables

Very simple question. I need to change url like
http://www.mysite.com/index.php?page=[x]
where [x] is any number to
http://www.mysite.com/index.php?id=[x]
Just to change ?page= to ?id= using .htaccess 301 redirect.
How can I do this?
Match the QUERY_STRING inside a RewriteCond:
RewriteEngine On
# Capture (\d+) into %1
RewriteCond %{QUERY_STRING} page=(\d+) [NC]
# And rewrite (redirect) into id=%1
RewriteRule ^index\.php$ /index.php?id=%1 [L,R=301]
The above would only rewrite requests to index.php. If you want to rewrite everything, instead use
RewriteRule ^(.*)$ /$1?id=%1 [L,R=301]

301 Redirecting Multiple Pages

My rewrite rule almost works but there are still some problems. Here is the code:
RewriteEngine on
RewriteRule ^tag/(.*)$ /?s=$1&search=GA [L,R=301]
The first problem now is that the redirect links to:
mydomain.com/?s=tag/&search=GA
How can I get rid of the second slash?
Now the second problem... when a tag contains more than 1 word (for example the tag marketing tips) the redirect is:
mydomain.com/?s=marketing-tips/&search=GA
How do I convert that - symbol to a + symbol?
Try this:
# getting rid of trailing slash:
RewriteRule ^tag/(.*?)/?$ /?s=$1&search=GA [L]
# change "-" with "+":
RewriteCond %{QUERY_STRING} ^s=([^&]+)-([^&]+)&(.*)
RewriteRule ^(.*)$ /$1?s=%1+%2&%3 [L,NE]
# if there's no more "-", redirect:
RewriteCond %{QUERY_STRING} ^s=([^&-]+)(&|$)
RewriteRule ^(.*)$ /$1 [L,R=301]
When I go to a URL like mydomain.com/tag/some-thing-else-lots-of-dashes/, I get redirected to mydomain.com/s=some+thing+else+lots+of+dashes&search=GA
It looks like your regex is picking up the slash from the incoming URL in it's collection, try moving it out and making it optional, also I made your selector less greedy:
RewriteRule ^tag/(.*?)/?$ /?s=$1&search=GA [L,R=301]

how can I do this rewrite with .htaccess

I have an url address like: example.com/?page=3
How can I write the .htaccess rewrite rule, to redirect such pages to example.com/pageid/3
Here "3" is just an example. I would like to make this redirect with any given number.
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)page=([0-9]+)
RewriteRule ^$ /pageid/%2 [L]
Replace the [L] with [L,R] if you want to redirect the browser, thus changing what's in the address bar to example.com/pageid/3 instead of redirecting internally.

Resources