Mod_rewrite: How to check word with special characters in URL - regex

I have some url's where somehow following keyword appearing
which causes url status to 404. I want to check using .htaccess that if url contains
this form eg. domain/<\?=BASEURL?>/other_part_of_url
then it will be converted to domain/other_part_of_url
I have tried with
RewriteRule /
for testing purpose I have checked for < only but it is not working.

Try this code in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteRule ^[^/]+/(.*)$ /$1 [L,R=301,QSA]

Try:
RewriteRule /<\?=BASEURL\?>/ /

Related

how do I solve a 404 error from a pretty url?

I'm trying to redirect my content removing the .php extension from all the files using this .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
and when I try to include any other redirect like this one give me a 404 error:
RewriteRule ^create/(.*)$ ./create.php?app=$1
I'm trying to use something like https://myurl.com/create/37744e17-98ff-58a9-8996-7cf746e508b9 instead of https://myurl.com/create.php?id=37744e17-98ff-58a9-8996-7cf746e508b9, but looks like the start of my .htaccess it's giving the main issue, there's a way to solve this?
You've not stated where you are putting that rule. It would need to go first. The order is important.
Try it like this instead:
# Disable MultiViews
Options -MultiViews
RewriteEngine On
RewriteRule ^create/([a-f0-9-]+)$ create.php?id=$1 [L]
# General extensionless ".php" URLs
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
Note that you used an id URL parameter in your example, but used app in your rule? I've changed this to id.
Based on your example URL, the id URL parameter can consist of the characters a-z, 0-9 and hyphens only.
The RewriteBase directive is not required. The ./ prefix on the substitution string is not required and should be removed.
In your original rule that appends the file extension there is no need to check that the request does not map to a directory before checking that it does map to a file (with a .php extension). You were also potentially checking a different file-path to the one being rewritten to.
Note that you need to ensure that MultiViews is disabled, otherwise create.php is called but without the URL parameter.

Is there any way to redirect to a clean url when old url is requested?

I want to change the URLs on my website from page.php?id=1&name=john to page/1/john using htaccess RewriteRule.
This is what I have currently but it is not working as expected:
RewriteRule ^page/([0-9]+)/([a-zA-Z\s-]+) page.php?id=$1&name=$2
Is it possible to make this change in htaccess rules or should I change every link to <a href='page.php/1/john'>Page</a> which is tiresome since I have got many links in every page. Help is appreciated. Thanks.
You may use these 2 rules in site root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /page\.php\?id=(\d+)&name=([^\s&]+)\s [NC]
RewriteRule ^ page/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^page/(\d+)/([^/]+)/?$ page.php?id=$1&name=$2 [L,QSA,NC]
1st solution: To get from query string URL to user friendly url try following, as per OP's request.
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
RewriteCond %{QUERY_STRING} ^id=(\d+)&name=([\w-]+)/?$ [NC]
RewriteRule ^([^.]*)\..*$ /$1/%1/%2/ [QSD,R=302,NC,L]
2nd solution: As far as I get from OP's question, could you please try following. Considering as per thumb rule users will be given friendly URL like eg--> http://localhost:80/page/1/john and it will point in pointed to http://localhost/page.php?id=1&name=john in backend.
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ /$1.php?id=$2&name=$3 [NC,L]

.htaccess to pass parameters and remove the page name

I have the following folder structure in my website:
example.com/
reports/
causelist/
final_causelist.php
I have a link / URL to the final_causelist.php page is as:
http://example.com/reports/causelist/final_causelist.php?wb_sno=23&dated=2020-10-26&j_names=Mr.+ABC
I want to access the page like the following:
http://example.com/23/2020-10-26/Mr.+ABC
For the above, I created .htaccess in the causelist folder with the following code:
RewriteEngine on
RewriteBase /reports/causelist/
Options -Indexes
RewriteRule ^(\d+)/([a-z-]+)/(.+)/$ /final_causelist.php?wb_sno=$1&dated=$2&j_names=$3 [L,B]
But it does not show the page and complaint that the requesting page is not found on this server?
Your regex is not matching date part correctly and you must remove leading / from target as you're using a RewriteBase.
Options -Indexes
RewriteEngine on
RewriteBase /reports/causelist/
RewriteRule ^(\d+)/([\d-]+)/(.+?)/?$ final_causelist.php?wb_sno=$1&dated=$2&j_names=$3 [L,QSA]
Could you please try following.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(\d+)/(\d{4}-\d{2}-\d{2})/(.+?)/?$
RewriteRule ^.*$ /reports/causelist/final_causelist.php?wb_sno=%1&dated=%2&j_names=%3 [QSA,NE,NC,L]

Fix the regex code in .htaccess for beautifying this url

my current regex code to beautify a url (www.abc.com/user.php?url="rio") in .htaccess file is
RewriteCond %{REQUEST_URI} user
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)$ user.php?url=$2
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/$ user.php?url=$2
It works fine and the page url is beautified to :
www.abc.com/user/rio
but there is a problem now, Now even if I change url like in following ways:
www.abc.com/user/user/user/rio
www.abc.com/user///rio
www.abc.com/user/abc/rio
These links work same way, and that is making content duplication.
Please help!
You may try these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# remove multiple slashes
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
# handles /user/something
RewriteRule ^user/([\w-]+)/?$ user.php?url=$1 [L,QSA,NC]
Options -MultiViews is required to turn off content negotiation feature.

htaccess rewrite URL and remove GET parameters

I am new to URL rewrite, regex and .htaccess
Here is an issue I am facing:
I have this url with GET parameter:
www.mysite.in/alpha-beta/abc.php?id=APPLE%strike=200.00
I want to display it like:
www.mysite.in/alpha/beta/APPLE/200.00
This is the code in .htaccess:
RewriteRule ^alpha/beta/(.*)/([0-9]+(\.[0-9]+))$ alpha-beta/abc.php?id=$1&strike=$2 [NC,L]
But I only get a blank page when I go to URL: www.mysite.in/alpha/beta/APPLE/200.00
When I change the htaccess rule to:
RewriteRule ^alpha/beta/(.*)/([0-9]+(\.[0-9]+))$ http://www.mysite.in/alpha-beta/abc.php?id=$1&strike=$2 [NC,L]
It redirects to correct page but the URL is displayed as http://www.mysite.in/alpha-beta/abc.php?id=APPLE&strike=200.00
What seems to be the problem?
Use these rules in root .htaccess:
RewriteEngine On
RewriteRule ^OI_TOOL/IV/(.*)/([0-9]+(?:\.[0-9]+))/?$ OI_ANALYSIS_TOOL/iv_chart.php?symbol=$1&str‌​ike=$2 [NC,L,QSA]
# fix path for getIV.php by redirecting it to /OI_ANALYSIS_TOOL/
RewriteRule ^OI_TOOL/.+?/(getIV\.php)$ /OI_ANALYSIS_TOOL/$1 [L,NC,R=301]