Hoping someone can help me here. In my .htaccess file, I've got a rewrite rule written as:
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
for a URL that would be something like
localhost/wrestling.php?id=something
And now what I'm trying to do is change a URL like
localhost/category.php?id=something
to
localhost/id/category
The thing to note with this scenario is that this time I'm trying to use the id part as the thing after the first "/" and then add the category part after the second "/".
Below is what my entire .htacces file looks like at the moment.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Any thoughts guys?
You can try
RewriteRule ^([^/]*)/category/?$ /category.php?id=$1 [L]
Try this :
RewriteRule ^([^\/]*)/([^\/]*)$ /$2.php?id=$1 [NC,QSA]
You need two captured parameters.
This expression was tested with http://htaccess.madewithlove.be/.
Related
I want to achieve the following
Any links like mydomain.com/anyword should be rewritten to index.php?pg=$1
example:
mydomain.com/new -->index.php?pg=new
mydomain.com/purchased -->index.php?pg=purchased
mydomain.com/login -->index.php?pg=login
Any links like mydomain.com/anyword/anynumber should be rewritten to index.php?pg=$1&id=$2
example:
mydomain.com/new/12 -->index.php?pg=new&id=12
mydomain.com/purchased/240 -->index.php?pg=purchased&id=240
mydomain.com/login/10 -->index.php?pg=login&id=10
I have used the following htaccess code.
The first one works properly.
Regarding the second one, the entire parameter is passed to pg and there is no parameter id at all.
That is in case of url
mydomain.com/purchased/240
in my php $_Get has only 'pg' parameter and 'id' parameter at all
and pg=/purchased/240
Please suggest the proper way to do it.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
RewriteRule ^([^/]+)/(\d+)/.*$ /index.php?pg=$1&id=$2 [NC,QSA]
Thanks
EDIT: Correct solution in case anyone made need it
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+) /index.php?pg=$1&id=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
This is because of the catch-all pattern. (.*) matches everything and rewrites it to the target. For your example, your first rule matches both requests, /foobar/ and /foobar/123 are rewritten to /index.php?pg=request . To fix this, you need to fix the order of your rules, you need to put your specific rule before the catch-all rule :
RewriteRule ^([^/]+)/(\d+)/?$ /index.php?pg=$1&id=$2 [L]
RewriteRule ((?!index.php).+) /index.php?pg=$1 [L]
I have a webpage with url website.com/app/ there is an index.php in the dir and also an .htaccess file.
I want a make this url into a clean url
website.com/app/?app=app1&id=12345
to
website.com/app/app1/12345
I was able to achieve this with this rule
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?app=$1&id=$2 [L]
RewriteRule ^([^/]+)index.php?app=$1 [L]
but I also want to be able to access the webpage with this url
website.com/app/app1
so that It'll mean website.com/app/?app=app1 But currently I get "Object not found!" when trying this.
Thank you!
Try this RewriteRule, it will leave you with the URL: website.com/app/app1
RewriteEngine On
RewriteRule ^app/([^/]*)$ /app/?app=$1 [L]
I'm answering my own qn here :
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /apps/?app=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?app=$1&id=$2 [L]
You're welcome
forcing this to work makes my kind of crazy so i hope you can help.
I use Rewrite Rules and .htaccess to make my dynamic URL
example.com/page.php?id=1
look like this
example.com/1
using
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
, and it works perfectly fine so far.
But i also want to hide the filetype in the URL ( impressum.php to impressum) using
RewriteRule (.*) $1.php [L]
So both Rules are working completely correct as long as i dont use them both at the same time. When i do so, which looks like this (my complete file)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
,i get an Internal Server Error. I tried different versions, for example change the positions and so on, but i allways get this error.
So my question is: how do i get both rules together and working, while the URL ending is still hidden and the example.com/1 works too?
Thank you very much for any answer
You can use the following in your .htaccess file:
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
You need two separate rules. Rewrite conditions will only get applied to the immediately following rule and with your php extension rule, you must check that the php file exists before adding the php to the end:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
I want to rewrite URL using .htaccess in codeigniter but it's not working. Can you please figure it out. Want to change URL from:
www.site.com/mauritius_holiday_rentals/apartment/anyname
to
www.site.com/mauritius_holiday_rentals/anyname
My current .htaccess file contains:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteRule ^mauritius_holiday_rentals/([A-Za-z0-9-]+)/?$ mauritius_holiday_rentals/apartment/$1 [L,QSA]
First four line is for removing index.php from URL which is working fine.
If routes file is set to access new url and you want to set redirection for old URLs then Use following code in .htaccess. otherwise let me know in detail what you want to do.
RewriteEngine on
RewriteBase /
RewriteRule /mauritius_holiday_rentals/apartment/$1 /mauritius_holiday_rentals/$1 [R=301,L]
Routes.php config file code
$route['mauritius_holiday_rentals/(:any)']="mauritius_holiday_rentals/apartment/$1";
Let me know if any problem.
RewriteEngine On
RewriteBase /yourProjectName/
RewriteCond $1 !^(images|stylesheets|javascript)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourProjectName/index.php?/$1 [L]
Try to use codeigniter routing system
https://www.codeigniter.com/user_guide/general/routing.html