I want to make my actual url:
http://mywebsite.com/portfolio.php?cat=deportes
Into:
http://mywebsite.com/portfolio/cat/deportes
I've been trying some solutions given on here but none of them work. I can actually get rid of the .php, but do I have to change anything in the a hrefs ?
Try
RewriteEngine on
RewriteRule ^portfolio/cat/(.+) /portfolio.php?cat=$1 [L]
starkeen gave you a nice answer. I would suggest to do it for your whole project, not to create rewrite rules for every url you have.
You can have a .htaccess file like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
And then to create your logic with php to parse the url itself and call the method / controller or whatever you wish.
Related
I am trying to rewrite a specific PHP file GET parameter but cant seem to get things to work.
I want to rewrite http://www.example.com/meeting.php?ref=y0fpjXrrGP so it is http://www.example.com/meeting/y0fpjXrrGP
What am I mising on the below? Note I am using WordPress so adding to the existing htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^meeting/(.*)$ meeting.php?ref=$1
</IfModule>
# END WordPress
Options -Indexes
Adding RewriteRule ^meeting/(.*)$ meeting.php?ref=$1 does not seem to work at all.
Just use this in your .htaccess file:
RewriteEngine On
RewriteRule ^meeting/([^/]*)$ /meeting.php?ref=$1 [L]
It will leave you with the URL: http://www.example.com/meeting/y0fpjXrrGP
Make sure you clear your cache when testing this.
You don't need to add that to .htaccess, if your URL is inside your Wordpress, you need to add them to the rewrite URL
Check this:
How to create custom URL routes?
Hope it helps.
I am trying to pass a variable from one page to another via my url. The structure of the urls looks like this http://localhost:8888/test_portfolio?location=ignite_rockford.
Here's the relevant info from my .htaccess file:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php?/$1?$1=$1
</IfModule></code>
The problem seems to be with this line: RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php?/$1?$1=$1, but I don't know what to do. If I add .php after test_portfolio, then it works, but otherwise when I dump $_GET['location'] I get NULL.
Oh yeah, I'm retrieving $_GET with $location = $_GET["location"];.
Any help would be appreciated.
If you are just trying to remove the .php, you can just add it and copy the query string with [QSA] tag:
RewriteRule ^(.*)$ $1.php [QSA]
Your URLs will go from:
/test to /test.php?/test?test=test are you sure this is what you want? (it looks malformed, among other things)
Usually you'll write a Router like:
RewriteRule ^(.*)$ router.php?__url=$1 [QSA]
This will give you a GET parameter __url with the original path. [QSA] will allow you to still accept all original query parameters, like location.
I need to convert a link that contains php variables into a clean link that can be picked up and indexed by Google. I've looked at ways to do this, and using mod_rewrite apparently is the best way to handle this.
Currently my URLs look like this:
http://mysite.com/jobs/view/?j=senior-model-validator-groep-risk-management
I'd like to end up looking like this
http://mysite.com/jobs/view/senior-model-validator-groep-risk-management
It's probably really easy but sadly I know nothing about .htaccess. I tried a few things but I end up with nothing remotely close :-)
Thanks in advance!
Edit: Here's my full .htaccess file based on the help so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You may use an .htaccess file at the root of you web site with these rules:
RewriteEngine on
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
Do not forget to filter/validate GET input in the script dealing with the data...
Hai
I have worked with clean URL in php. Now I want to convert a clean URL to normal php URL Like
http://localhost/url/user/2/a to
http://localhost/url/user.php?id=2&sort=a
Can any one give me the way to do this?
also i have one more question. Is there is any way to do this with out .htaccess?
In your .htaccess file in your root directory:
RewriteEngine On
RewriteRule ^url/user/(\d+)/([a-zA-Z]?)$ /url/user.php?id=$1&sort=$2
should do it.
I'd suggest not to write specific rule for the every module, but make a front controller which will receive all requests and the dispatch them to the corresponding modules.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [QSA,L]
So, you'll end up with $_GET['uri'] parameter in your script, which can be parsed to get required values
i have urls like
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938
but i want urls like
http://mysite.com/resources
http://mysite.com/resources/view/938
instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php
but how? could someone give an example or link a tutorial
I happen to use this in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.
The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.
Create a .htaccess file. Not somefilename.htaccess, it is simply named .htaccess.
Note: Your index.php and .htaccess file should be in the same directory.
Now try this on your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>
see more about url rewrite here