How to ignore URL at the end of URL - apache

I have a URL
original: https://www.example.com/video/12345/name-of-the-video/
Sometimes users or web crawlers write the URL differently:
https://www.example.com/video/12345/name-of-the-video/65485/
https://www.example.com/video/12345/
https://www.example.com/12345/
I want all 3 URLs to redirect (forward) to the original:
https://www.example.com/video/12345/name-of-the-video/
My current htaccess rule is:
RewriteRule ^video/([0-9]+)/([^/]+)/$ view_video.php?id=$1&dir=$2 [L,QSA]
RewriteRule ^video/([0-9]+)/$ view_video.php?id=$1&dir=fake [L,QSA]

Could you please try following, written based on your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##To rewrite URL https://www.example.com/12345/
RewriteRule ^([0-9]+)/?$ view_video.php?id=$1&dir=fake [NC,L]
##To rewrite URL https://www.example.com/video/12345/
RewriteRule ^video/([0-9]+)/?$ view_video.php?id=$1&dir=fake [NC,L]
##To rewrite URL https://www.example.com/video/12345/name-of-the-video/65485/
RewriteRule ^video/([0-9]+)/([\w-]+)/[0-9]+/?$ view_video.php?id=$1&dir=$2 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/view_video\.php [NC]
RewriteCond %{QUERY_STRING} id=([^&]*)&dir=(.*) [NC]
RewriteRule ^(.*) video/%1/%2 [R=301,L]
##To rewrite URL https://www.example.com/video/12345/name-of-the-video/
RewriteRule ^video/([0-9]+)/([\w-]+)/?$ view_video.php?id=$1&dir=$2 [NC,L]

Related

Need to redirect 301 a URL in .htaccess file but it adds extra http//?

I am trying to redirect /abc.html to /abc.php but when I did it gives an extra http// and page is not working like http//www.example.de/abc.php don't know from where this HTTP comes.
note: website is not with ssl so domain name is http://example.de
My .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.de/$1 [R=301,L]
RedirectPermanent /tour.html /tour.php
With your shown samples/attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
##To serve home page link.
RewriteRule ^/?$ index.php [L]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^([^.]*)\.html/?$ $1.php [NC,L]

how to Rewrite two parameters URL to SEO friendly URL and Redirect to SEO friendly url

This is my page url example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs
These rules helped me to Rewrite this as example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php?pID=$1&name=$2
RewriteRule ^platform/bidProject.php?pID=$1&name=$2 /platform/project-bids/(.*)/(.*)/ [NC,R=301,L]
but the issue is if I visit to the page example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs the url stays same. I want this to be redirected to example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/
So I tired this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ /platform/bidProject.php?pID=$1&v=$2 [L,QSA]
Seems I am doing something wrong with this because it makes no any affect on this.
I found a solution to redirect using JavaScript but I like to have .htaccess solution because JavaScript can be disable and can be seen in the source code. The basic intention for doing this fails here.
How can I achieve this using .htaccess
My htaccess path is example.com/.htacsess
You can use these Rules
RewriteEngine on
#redirects /platform/bidProject.php. php?pid=val1&name=val2 to /platform/project-bids/val1/val2/
#redirects the old url to the new one
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php\?pID=([^&]+)&name=([^&\s]+) [NC]
RewriteRule ^ /platform/project-bids/%1/%2/? [NC,R,L]
# rewrites or internally maps the new url to the old one
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
Or
RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^pid=([^&]+)&name=([^&]+)$ [NC]
RewriteRule ^platform/bidProject.php$ /platform/project-bids/%1/%2/? [NC,R,L]
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
Change R to R=301 (permanent redirect) when you are sure the rule is working ok.

RewriteRule htaccess friendly url

I'm trying to make firendly urls.
My site url is:
domain.pl/index.php?str=prod_big&idk=869&kat=
My .htaccess code is:
RewriteCond %{HTTP_HOST} !^domain\.pl$
RewriteRule ^(.*) http://domain.pl/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?str=$1&idk=$2&kat= [L]
But after visiting:
http://domain.pl/prod_big/869.html
nothing happens. What should i do more ?

How to do a specific condition for escaped_fragment with rewrite rule in .htaccess

I have urls like that /#!/page1, I redirect them with :
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F(.*)$
RewriteRule ^$ /seo/%1.html [QSA,L]
So /#!/page1 read as /_escaped_fragment_=%2Fpage1 is redirected to /seo/page1.html
It's work perfectly however I want to redirect the home /#!/ to /seo/index.html which actually redirect to /seo/.html
How can I do that?
Thanks
I fixed it with:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F$
RewriteRule ^$ /seo/index.html [QSA,L]
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=%2F(.*)$
RewriteRule ^$ /seo/%1.html [QSA,L]

.htaccess redirect 301 using rewrite rule

I'm trying to beautify some urls. I configured the htaccess file so my urls are changed:
old url: http://mysite.com/index.php?id=45tye4
new url: http://mysite.com/45tye4
I want now to permanently redirect old urls to new urls. This is what I try with no luck:
RewriteRule ^index.php?id=(.*)$ $1 [R=301,L]
The main problem seems to be the '?' in the url. When I try the same url without ? the redirect works. I also tried other variants with no luck:
RewriteRule ^index.php\?id=(.*)$ $1 [R=301,L]
RewriteRule ^index.php[\?]id=(.*)$ $1 [R=301,L]
Update:
I added the redirection according to anubhava instructions. The redirection works, but unfortunately I get into a redirect loop. I thought [L] flag should solve the redirection loop, but it doesn't.
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^index\.php/?$ /%1? [R=301,L]
RewriteRule ^(.*)$ index.php?id=$1 [L]
RewriteRule matches only REQUEST_URI. You have to use RewriteCond to match Query string
Try this code:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/+index\.php [NC]
RewriteCond %{QUERY_STRING} (^|&|\?)id=(.*)(&|$) [NC]
RewriteRule . /%2? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L]
This will redirect old URI of /index.php?id=45tye4 to new URI: /45tye4