Rewrite Querystring to File - .htaccess

I mirrored our Basecamp install to a Google Cloud Disk using wget. Many of the pages that were created are using the following file names:
Files
files.html?page=4
files.html?page=5
files.html?page=6
files.html?page=7
files.html?page=8
files.html?page=9
index
log?n=0
We were able to fix the index issue with log%3fn=0:
DirectoryIndex index.html index.htm log%3fn=0 log report new_more new
I wanted to see if there were a way to add a rewrite in .htaccess that will make
000.000.000.000/projects/11424851-m-domain-com/files.html?page=2
resolve to the file name that contains a query-string in it.
I am thinking the rewrite would have to produce:
000.000.000.000/projects/11424851-m-domain-com/files.html%3fnpage=2
Thank for any help!!

You can use something like:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^(.*)$ %{REQUEST_URI}\%3Fnpage=%1? [L]
I.e. match query strings starting with page=, and redirect to the same filename with the query string appended in escaped form.

Related

Rewrite engine not working using htaccess

I am trying to rewrite url using htaccess but it's saying 404.
RewriteEngine on
RewriteRule ^p/([a-z0-9])$ product1.php?pid=$1
It's not opening product1.php file.
You need to make sure your htaccess file and your product1.php file are in same folder. If not that it will give 404 errors.
If they are not present in same folder then you need to complete relative path from the path where your htaccess file is present like: eg: Let's say we have folder structure like:
/root/.htaccess
/root/singh/test/product1.php
So have your rules in following manner then:
RewriteEngine on
RewriteRule ^p/([a-z0-9])$ singh/test/product1.php?pid=$1 [NC,L]
Also apart from these put NC and L flags too in your rules to do case sensitive matching and making this condition's last rule here.

How to use htaccess file for subdirectory url

My problem is to redirect from subdirectory link to one file.
my current url
www.sample.com/stream/34/video.m3u8
www.sample.com/stream/35/video.m3u8
hope
www.sample.com/stream.php?param1=35&param2=video.m3u8
or
www.sample.com/stream/index.php?param1=35&param2=video.m3u8
Please save my life
You can do something like the following using mod_rewrite in the root .htaccess file in order to rewrite the request to /stream.php?param1=<id>&param2=<file>:
Options -MultiViews
RewriteEngine On
RewriteRule ^stream/(\d+)/([\w-]+\.\w{2,4})$ stream.php?param1=$1&param2=$2 [L]
This assumes that the <id> (2nd path segment) is entirely numeric and the 3rd path segment consists of <filename>.<ext>. You should restrict this to m3u8 if that is all you are expecting.

URL rewriting without knowing page title

I currently have links at my site like this:
http://www.domain.com/locations/locationProfile.php?locationID=22
I am hoping, for SEO purposes that I could change this to something like this:
http://www.domain.com/locations/southern-maryland
"Southern Maryland" is currently pulled from a mysql db as the location name for location ID 22. Is it even possible to get this into the URL when my site structure currently utilizes the less attractive first version?
You can't use htaccess to do this for you. It's possible to use a RewriteMap that you can define in your server config (can't define it in htaccess file), but it's far easier if you just do this in your locationProfile.php script.
You can detect if the request is made with the query string using the REQUEST_URI value:
if ( $_SERVER['REQUEST_URI'] == '/locations/locationProfile.php' &&
isset($_GET['locationID'])) {
// go into DB and extract the location name then redirect
header('Location: /locations/' . $location-name);
}
Then in your htaccess file in your document root):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^locations/([^/]+)$ /locations/locationProfile.php?locationName=$1 [L,QSA]
And finally, make your php script look for the locationName parameter and server the proper page based on that instead of the ID.

Url rewriting with parameters

I’m a noob in url rewriting and i really need help to figure this out :
I have this url : section/news.php?url=section/news/27-how_to_make_rewrite.html I want to access this news with the url parameter and the link would be : section/news/27-how_to_make_rewrite.html
the .htacess file is in the root, i have a folder named section and inside this folder i have the news folder
This doest work for me.
I have :
RewriteEngine on
RewriteBase /section/
RewriteRule news/^([a-zA-Z0-9-+/]+)/$ news/news.php?url=$1
How I can do it ?
I have not used this with subfolders, but I would try something like this.
RewriteRule ^news/([^/\.]+)/?$ news/news.php?url=section/news/$1 [L]
Edit
Try it this way:
RewriteRule ^section/news/([^/.]+)/?$ news.php?url=section/news/$1 [L]
You normally only want to put what is changing in the variable. section/news/ is always used so you should keep it outside of the replacement.
If the URI you want to rewrite to looks like: section/news.php?url=section/news/27-how_to_make_rewrite.html
then you've got one too many "news" in your target. You also need to fix your regex pattern to match stuff like 27-how_to_make_rewrite.html.
You want something like:
RewriteEngine on
RewriteBase /section/
RewriteRule news/(.+)/?$ news/news.php?url=section/news/$1

Redirect any url containing /foo/

I'm trying to redirect a series of around 400 urls using .htaccess/Apache containing a given /directory/ anywhere in the url to a specific location.
The problem here is that my site is receiving requests for an old site hosted on our servers ip. I've tried manually redirecting the urls but the volume is simply too great.
I've searched but can only find examples for redirecting query strings or files
Thanks in advance.
Ok, if all links have the same directory in there... example store/funstuff/blahblah.php
and funstuff is the directory you are looking for then you could modify your .htaccess file something linke this
Options +FollowSymlinks
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_URI} funstuff
RewriteRule . http://www.gohere.com/
Then if you needed to pass more of the URL info you could do the last line like this:
RewriteRule ^(.*)$ http://www.gohere.com/1$
That should get you started... you may need to tweak it slightly.
What language are you processing the redirects in?
You probably need to use a regular expression that searches for the given /directory/.
If I understand your question correctly and that you are using Apache; RedirectMatch should do what you want.
It accepts a regexp for matching and can then redirect to the place you choose.

Resources