.htaccess redirect with condition - .htaccess

here is my rules for htaccess file.
RewriteRule ^admin/?$ admin/index.php
RewriteRule ^cart/([0-9]+)/?$ cart.php?t=$1
RewriteRule ^register$ register.php
RewriteRule ^login$ login.php
RewriteRule ^forgot-password$ forgot_password.php
RewriteRule ^terms-and-conditions$ terms_and_conditions.php
RewriteRule ^request-consideration$ request_consideration.php
RewriteRule ^([a-z0-9\-]+)/?$ products.php?cid=$1
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ products.php?cid=$1&scid=$2
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ product_details.php?cid=$1&scid=$2&id=$3
ErrorDocument 404 /error_404.php
Now the problem is when i try to redirect it say www.example.com/cart/123 it goes products.php page. I want to restrict the url www.example.com/cart/123 only. so www.example.com/cart or www.example.com/cart/abc will redirect to error_404 page. same for other also like register, login etc.
Any help would be appreciated. Thanks in advance.

Based on you comments, you want to redirect the uri to 404 page if /cart or /cart/abc is requested, you can use the following rules. Put this at the top of your htaccess before other RewriteRules
RewriteEngine on
RewriteRule ^cart/?$ - [R=404,L]
RewriteRule ^cart/[A-Za-z]+/?$ - [R=404,L]

Related

Rewriterule for PHP files fails

I want to rewrite these .htaccess rules into a templates to use it for my other pages as well:
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
For example:
RewriteRule ^admin/([a-z-])/?$ admin/$1.php [L]
RewriteRule ^admin/([a-z-])/([0-9a-z-#._]+)/([0-9]+)/?$ admin/$1.php?name=$2&id=$3 [L]
I tested the latest rules on WAMP and it does not work. It returns: 404 Not Found error
The requested URL was not found on this server.
The full .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
Files locations are:
1. C:\wamp64\www\test.com
2. C:\wamp64\www\test.com\admin\add_news.php
C:\wamp64\www\test.com\admin\edit_news.php
3. I want redirect from URLs with underscores to hyphens, for example:
from admin/add_news.php to admin/add-news
Also from admin/edit_news.php?name=%1&id=%2 to admin/edit-news/name/id
Example: admin/edit-news/my-news-title/2
Any ideas how to fix it? Thank you.
With your shown attempts, please try following htaccess rules file.
Make sure that your .htaccess is present along with your admin folder AND make sure to clear your browser cache before testing your URLs.
Samples specific rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([\w-]+)/?$ admin/$1.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([\w-]+)/([^/]*)/(\d)+/?$ admin/$1.php?name=$2&id=$3 [QSA,NC,L]
Generic rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/?$ admin/$1_$2.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/([^/]*)/(\d)+/?$ admin/$1_$2.php?name=$3&id=$4 [QSA,NC,L]

Redirected URL through .htaccess is being redirected to the different location

My .htaccess has the following code :
RewriteRule ^([A-Z]+)$ listings/$1 [R=301]
RewriteRule ^listings/([A-Z]+)/?$ listings_state.php?state=$1 [NC,L]
With this rule I am trying to achieve 301 redirect from spb.com/test_1/XY to spb.com/test_1/listings/XY
However, it is redirecting to spb.com/path/to/test_1/in_server/test_1/listings/CT
and I tried changing .htaccess to as follows:
RewriteRule ^([A-Z]+)$ /listings/$1 [R=301]
RewriteRule ^listings/([A-Z]+)/?$ listings_state.php?state=$1 [NC,L]
RewriteRule ^/([A-Z]+)$ /listings/$1 [R=301]
RewriteRule ^listings/([A-Z]+)/?$ listings_state.php?state=$1 [NC,L]
RewriteRule ^/([A-Z]+)$ /listings/$1 [R=301]
RewriteRule ^/listings/([A-Z]+)/?$ listings_state.php?state=$1 [NC,L]
Nothing seems to work.
Please guide me in this regard. Thank you community :)
Could you please try following, based on your shown samples only. Please make sure you clear your browser cache before testing your URLs
RewriteEngine ON
RewriteRule ^([A-Z]+)/?$ /test_1/listings/$1 [R=301]
RewriteRule ^test_1/listings/([A-Z]+)/?$ listings_state.php?state=$1 [NC,L]

404 page not working on website, help me to sort out Htaccess issue

I have removed this post from our database entirely but the below link is still showing blank template instead of 404 not found error,
http://www.myconsumercourt.com/complaints/online-shopping/men-clothing/complaint-against-livastar-backlift-technologies-pvt-ltd-1421.html
here is my .htaccess code
RewriteEngine on
ErrorDocument 404 /404.php
RewriteRule ^(.*)/$ /$1 [R,L]
RewriteRule ^complaints/(.*)/(.*)/(.*)-(.*).html$ post-info.php?ticket-number=$4 [L]
RewriteRule ^complaints/(.*)/(.*)$ query-listing.php?sub-category=$2 [L]
RewriteRule ^complaints/(.*)$ subject-list.php?category=$1 [L]
Please help me to redirect my broken page to 404.php page.
Thank you so much.
Kind Regards,
This is because your first RewriteRule is rewriting this url to an existent file.
put the following rule bellow "RewriteEngine on" line to redirect the url to 404.
RewriteCond %{THE_REQUEST} /complaints/online-shopping/men-clothing/complaint-against-livastar-backlift-technologies-pvt-ltd-1421\.html [NC]
RewriteRule ^ - [L,R=404]

.htaccess of symfony make a 302 redirection

Using symfony for my website and i have a 302 redirection from my https://www.exemple.com/ to https://www.exemple.com/, and google doesn't want 302 redirection... It is the last condition of the rewrite section of the .htaccess which does that.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
I tried to put instead :
RewriteRule .? %{ENV:BASE}/app.php [R=301,L]
But in that case i have a 301 redirection but my site is not reachable. Any ideas ?
Find solution,
It was not the .htaccess in cause. I'm new with 301/302 redirection and didn't know u can spécifiate it in controller. So i add a ",301" at the end of redirections i do in controller like that :
return $this->redirect($this->generateUrl('sdzbikind_acceuil'), 301);
Hope it helps somebody else... I'm just asking now if i should do 301 redirection in all my symfony project redirections !?

Url rewrite and trim url

I need your precious help.
I have an url:
http://domain.com/v/12345
My ht access is set to pass 12345 to php script like:
htaccess:
RewriteRule ^v/([a-zA-Z0-9_-]+)$ /script.php?v=$1 [L]
script php:
http://domain.com/script.php?code=12345
and works perfectly but.. and there is a but, I would write my url so:
http://domain.com/12345/my-dog-is-very-fat
can you help to change my htaccess to works so? Because if I use /my-dog-is-fat or just / I get an 404 error.
I try to explain better:
I would share link with seo keywords in the url, example
mydomain.com/alphanumericCode/thi-is-my-dog-article
where alphanumericCode is get of myscript.php?V=
This is my htaccess:
RewriteEngine On
RewriteRule ^view/([a-zA-Z0-9_-]+)$ /view.php?v=$1
RewriteRule ^folder/([a-zA-Z0-9_-]+)$ /filefolderlist.php?f=$1
RewriteRule ^home$ /index.php [L]
RewriteRule ^report$ /report.php [L]
RewriteRule ^play/([a-zA-Z0-9_-]+)$ /play.php?v=$1 [L]
###Added by anubhava
RewriteRule ^v/([\w-]+)/?$ /script.php?v=$1 [L,QSA]
RewriteRule ^([0-9a-z]+)/.+$ /script.php?v=$1 [L,QSA,NC]
##end
RewriteRule ^privacy-policy$ /privacy.php [L]
ErrorDocument 404 404.php
You can have a new rule for the new URI scheme:
RewriteEngine On
RewriteRule ^v/([\w-]+)/?$ /script.php?v=$1 [L,QSA]
RewriteRule ^([0-9a-z]+)/.+$ /script.php?v=$1 [L,QSA,NC]

Resources