How to correct this rewrite rule? - php

I have url as
http://www.mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c. I need to change this url to http://www.mydomain.com/medium.
I am not familiar with rewrite url.
I tried with RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c, but not worked correctly.
Full rewrite rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteRule ^(.*)$ index.php [QSA,L]

Try to change only that line to:
RewriteRule ^medium /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Or if you don't care if anything is after medium is the following:
RewriteRule ^medium(.*) /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c

Your rules are in the wrong order, try changing them to:
RewriteEngine On
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
The conditions need to be applied to the index.php rule, and not the medium rule. RewriteConds are only applied to the immediately following RewriteRule, not all of them.

This rewrite rule worked fine
RewriteRule ^medium.*$ /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c [R=301,L]

Related

Rewrite .htaccess file but not working

I wish to rewrite this url:
http://salessurface.com/overseas/sub_page_details/MBBSAbroad/MBBS-in-UK/7
into:
http://salessurface.com/overseas/sub_page_details.php?sup_menu=MBBS%20Abroad&menu=MBBS-in-UK&main_menu_id=7
I tried adding the following to the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ overseas/sub_page_details.php?sup_menu=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ overseas/index.php?sup_menu=$1&menu=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ overseas/index.php?
sup_menu=$1&menu=$2&main_menu_id=$3 [L]
but it is not working.
How do I accomplish that rewrite?
You don't need the conditions, you can do that with just a rule:
RewriteEngine on
RewriteRule ^overseas/sub_page_details/(.*)/(.*)/(.*) /overseas/sub_page_details.php?sup_menu=$1&menu=$2&main_menu_id=$3 [NC,L]
This will take the path after sub_page_details and put the first part in sup_menu, the second part in menu and third part in main_menu_id.
But I noted that in your sample URLs you use MBBS%20Abroad in the real URL but MBBSAbroad in the rewritten URL, if this is not a typo and the real URL has an space on it that doesn't have the rewritten URL then you need to use a less specific rewrite for that option:
RewriteEngine on
RewriteRule ^overseas/sub_page_details/MBBSAbroad/(.*)/(.*) /overseas/sub_page_details.php?sup_menu=MBBS\ Abroad&menu=$1&main_menu_id=$2 [NC,L]
This will hard code the sup_menu part of the URL and take the path after MBBSAbroad and put the first part in menu and second part in main_menu_id.

How to rename part of a url with .htaccess?

I currently have very little Apache experience, and am having difficulties with my .htaccess file. My question is this: how can I rename these files, listed below, properly? I believe my syntax is accurate, according to http://www.htaccesscheck.com, but when accessing these pages, either A: the page won't load due to a redirect loop, or B: the page won't load, but will redirect to the wrong page. Here is my current .htaccess file for this directory:
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Any help is much appreciated.
Try code below:
RewriteEngine on
RewriteBase /photos/
RewriteRule ^([0-9]{1,2})-([0-9]{4})$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([0-9]+)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
This rules will check, if:
request like yourdomain/11-1111, then return archives.php
request like yourdomain/111, then return catpost.php (you can type
any number)
else will return viewpost
You have some errors in your current .htaccess, because your second rule get result of first rule.
By the way, you can use http://htaccess.madewithlove.be/ to check step by step what is posted to your rewrite rules.
be sure to write a valid pattern
try this
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*?)-(.*?)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*?)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*?)$ viewpost.php?id=$1 [QSA,L]

Getting rid of GET keys parameters in the URL, keep only values

I wanna to get rid of GET string parameters, from keys.
For example this is initial uri:
http://example.com/api/get_users_method?user_age_from=18&user_age_to=25&user_city=ohio
http://example.com/api/get_users_method?user_age_from=18&user_age_to=25
http://example.com/api/get_users_method?user_city=ohio
I wanna make like this:
http://example.com/api/get_users_method/18/25/ohio
http://example.com/api/get_users_method/18/25
http://example.com/api/get_users_method/ohio
How can i get this result? I found some solution, to edit htaccess file, but it doesn't work unfortunately, maybe i made some mistakes in htacces file? I'm using php. Thanks!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /api/get_users_method?user_age_from=$1&user_age_to=$2&user_city=$3 [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /api/get_users_method?user_age_from=$1&user_age_to=$2&user_city=$3 [NC,L]
You have to treat the rules separately. All Conditions preceding rules only apply to a single rule. Following rules are not touched by that rule.
You will 3 separate rules in your root .htaccess (a level above api directory)
Options -MultiViews
RewriteEngine On
RewriteRule ^(api/get_users_method)/(\w+)/(\w+)/(\w+)/?$ $1?user_age_from=$2&user_age_to=$3&user_city=$4 [NC,L,QSA]
RewriteRule ^(api/get_users_method)/(\w+)/(\w+)/?$ $1?user_age_from=$2&user_age_to=$3 [NC,L,QSA]
RewriteRule ^(api/get_users_method)/(\w+)/?$ $1?user_city=$2 [NC,L,QSA]

htaccess rewriting rules not working

I am trying to rewrite the following url
http://localhost/foldername/index.php?url=writeboard/index&step=1&id=1
to
http://localhost/foldername/writeboard/index/step/1/id/1
Code is
RewriteRule ^(.+?)(?:/(step)/([0-9]+))?/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
I tried with
RewriteRule ^(.+?)(?:/(step)/([0-9]+))(?:/(id)/([0-9]+))?/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
it works but when the url becomes http://localhost/foldername/writeboard/index then I am getting 404.
This rule should work:
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?url=$1 [NC,L,QSA]
You should also add RewriteBase /foldername/ to your .htaccess file, since you're not in the top level.
More information about RewriteBase can be found here.

.htaccess rewrite with codeigniter

I've got a codeigniter setup where all URLs redirect to index.php, like below.
I need /api/index.php to go to /api, but NOT through a redirect.
My code below matches api/index.php but it is still not working.
/index.php/api works fine, and goes to the correct URL, however /api/index.php just goes to a 404 error page... Any suggestions? Am I being silly?
RewriteEngine on
RewriteBase /
RewriteRule ^api/index.php$ /index.php/api [L]
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
RewriteRule ^(.*)$ /index.php/$1 [L]
Probably because it is looking for a file named /api/index.php
Add these Rewrite Conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
//Your rewrite rule here
Try this:
RewriteEngine on
RewriteCond $1 !^(index\.php|img|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Categories