mod_rewrite with ? in the original URL like YouTube - php

Ok I want to simulate the YouTube URL and redirect it to my real URL. Reason being is I used a random string to make video ID's so they aren't guessable by sequence.
I have the links as so
http://www.mysite.com/watch?v=Dxdotx3iT1
and want to redirect to
http://www.mysite.com/index.php?page=videos&section=view&v=Dxdotx3iT1
Can't seem to figure out the mod rewrite. Also using a ? I believe tells it to do something.
Any help would be appreciated.

You need to adjust your RewriteRule to include the query string using the [QSA] (query string attached) flag:
RewriteRule ^watch$ index.php?page=video&section=view [QSA]

RewriteCond %{QUERY_STRING} ^v=(.+)
RewriteRule ^watch /index\.php?page=videos&section=view&v=%1 [QSA,R=301,L]

None of these answers worked for me, so in the end I found through trial and error the following worked for me;
The RewriteRule to get my URL to look like this https://www.example.com/video/watch?v=UnIq3Id follows;
RewriteRule ^video\/watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/UnIq3Id interfered with my redirecting;
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
The RewriteRule to get my URL to look like this https://www.example.com/watch?v=UnIq3Id is as follows;
RewriteRule ^watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/ interfered with my redirecting;
RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Hope it helped someone and saved you the headache I had, people are not so forthcoming on this issue at times How do you write an htaccess RewriteRule to make my urls work like youtube?.

Related

Unable to remove query strings from URL using .htaccess

My URLs look like http://example.com/?n=x. I want the URL to show as http://example.com/. I have used to approaches so far and none of them works.
First one is based on this question:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=1$
RewriteRule (.*) $1? [R=permanent]
After the answer below I modified the .htaccess file:
```RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule (.*) $1? [R=permanent]```
but it still did not work. Here is the debugging info:
RewriteCond %{QUERY_STRING} ^n=(.*)$
This condition was met.
RewriteRule (.*) $1? [R=permanent]
The new url is http://example.com/blog/cat/??
Test are stopped, a redirect will be made with status code permanent
Second approach is:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ $1 [QSD]
None of them seem to work.
I have a few more questions:
Does rewriting only work if the URL is not typed manually?
Can I still access the values of query strings in PHP?
What happens when a user copies the rewritten URL?
The first approach is correct if you go to http://example.com/?n=1, if I am correct you should change ^n=1$ to ^n=(.*)$.
And the other questions:
It works for all kind. It doesn't matter if it was a robot or a human writing it, when you access a page, .htaccess will be read.
Yes you can. Use $_SERVER["QUERY_STRING"]. If you use the htaccess redirection before explained, you will lose them because you are redirecting.
What do you mean ?

How to removing part of url by htaccess redirect rule

For some reasons a google addbot generates links like:
https://batteryservice.bg/продукт/фенер-nitecore-mt40gt/batteryservice.bg
I would like to remove this last part /batteryservice.bg and have something like:
https://batteryservice.bg/продукт/фенер-nitecore-mt40gt/
I tried this solution like : RewriteRule ^(.+)/batteryservice.bg /$1 [R=301,L,NC] But this didn't worked for me.
This one worked, strangely enough RewriteRule ^(.+)/batteryservice.bg https://batteryservice.bg/$1 [R=301,L,NC]

Rewriting The URL With .htaccess

Ok, So I've looked at this topic for quite a while now and can't get anything to work, probably because I'm still having difficulty understanding it - So I'm going back to basics and asking this in the simplest of terms.
I have an empty .htaccess file
I have a current URL of http://www.website.co.uk/news.php?id=111111
I want this to become http://www.website.co.uk/news/111111
How Do I Do This?
Also please not that although this is the URL now, I'm planning on making some changes to the site so the URL's in the future may be:
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111
http://www.website.co.uk/news/city/issue/the-title/111111
How can I make it so that the future changes will work too? So far I have:
RewriteEngine On
RewriteRule ^news/(.+)$ news.php?id=$1 [L]
This still displays the full url and typing in news/111111 redirects to an error page. Please help!
Adding the following to your htaccess should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^news.php$ /news/%1? [L,R]
RewriteRule ^news/(.*) news.php?id=$1 [QSA]
The above will change http://www.website.co.uk/news.php?id=111111 to http://www.website.co.uk/news/111111
and below will change
RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R]
RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA]
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111 into http://www.website.co.uk/news/city/issue/the-title/111111
The values in %1, %2, $3, %4 gotten from the parameters after city=, issue=. title=. id=.
In city=London, London will be contained in %1 etc
The second RewriteRule will allow you to find the id used.

URL Mod Rewriting

I am using codeigniter for my website, and before theres always that index.php? on my every url or links, for example
mysite.com/index.php?/about
Google has indexed all of my urls with that index.php? and I want to remove it and redirect it without that. I am having a problem rewriting the URL and redirect it to mysite.com/about and this what i have tried so far
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [QSA,L,R=301]
what happened is, it only removed the index.php,
for example mysite.com/index.php?/about will turn to mysite.com/?/about I don't know how to remove that question mark,
I'm not good on mod_rewrite thanks in advance for the help.
I think you can improve the rules slightly.
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} index\.php?\?.+
RewriteRule .*$ %{QUERY_STRING}? [R=301,L]
Essentially, you don't have to worry about the entire request line in %{THE_REQUEST}, which removes all the complicated regex. Also, the rule redirects to whatever is listed in %{QUERY_STRING}, and removes the query string.
I am not sure why you used QSA in the first place. I think that was part of the problem earlier. Just for an exercise, you can try removing QSA and see what happens.
You should try this one.
RewriteCond %{QUERY_STRING} ^/[a-z]+$ [NC]
RewriteRule ^/index.php$ %{QUERY_STRING} [NC,L,R=301]

php how to replace ?page=1 to 1

I'm just wondering how other websites replace there get variable.
example:
http://www.example.com/page.php?page=1
replaced:
http://www.example.com/page/1
indeed, just what ssx said.
You need a .htaccess file in the root of the website, with some content that looks like this (i use this one)
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([\(\)\|a-zA-Z0-9_-]+)/([0-9]+)/?$ /index.php?ext=$1&cat=$2&name=$3&urlID=$4 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([\(\)\|a-zA-Z0-9_-]+)/?$ /index.php?ext=$1&cat=$2&name=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/p=([0-9]+)/?$ /index.php?ext=$1&cat=$2&p=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /index.php?ext=$1&cat=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?ext=$1 [L]
I'd say take a look at an answer I provided someone else a while back. I'd type it all out here again or copy and paste it but the link is much easier.
PHP dynamic DB page rewrite URL

Categories