I'm trying to display my get variables as flat links.
Want to change from this:
http://mydomain.com/index.php?page=shop&var1=hat&var2=10
to
http://mydomain.com/index.php/shop/hat/10
Please keep in mind that there isn't a fixed number of variables, that's why I use var1,var2,...etc
[edit]
I already have a partially working script, but works only with max 3 variables
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2&var2=$3 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2&var2=$3&var3=$4 [L]
So first remember mod_rewrite works like this :
http://mydomain.com/index.php/shop/hat/10
(what the client type) is rewritten to
http://mydomain.com/index.php?page=shop&var1=hat&var2=10
(what the client is served), but not necessarily displayed like the latter. (Unless you make it a redirect)
So assuming your format is completly described here :
RewriteRule ^index\.php/([^/]+)/([^/]+)/([0-9]+)$ /index.php?page=$1&var2=$2&var2=$3
Should be good.
EDIT:
Oh BTW! I did not take the variable number of variable into account. That should not be processed by mod_rewrite I think. Maybe the best shot is to RewriteRule index.php/(.*) /index.php?call=$1 and then use your script to explode using / delimiter.
You can only do it if you already know the number of variable only AFAIK.
You could just direct all request directly to index.php (called bootstrapping), and let the script parse out the variables.
This is how it's done with Zend Framework, you should take a look at it. ZF also has loads of other goodies you could utilize.
This is my rewrite block from one of my vhosts with a ZF powered website.
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon.ico
RewriteRule ^.*$ index.php [NC,L]
Related
I have a website where i do blogging... on my blog.php page i have posts from different categories.
when a user clicks on a link it is being redirect to /blog-details.php page with $_GET['title'] variable.
what I have tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
last line gives me something like
xyz.com/blog-details?title=hello-world
what i want to achieve is
xyz.com/blog/hello-world
not like
xyz.com/blog-details?title=hello-world
thank you, looking forward to answers.
An important thing you have to take into consideration when working with .htaccess files, is that you have to check if the path is a directory or file, then proceed to do your action.
I used your code and it worked properly, but simply added the -f and -d checks to make sure that this does not affect other pages.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#remove .php from all pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
#blog/title-here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ /blog-details.php?title=$1 [NC,L,QSA]
To test out your .htaccess file and with some dummy URLs, I'd highly recommend you to check out MadeWithLove
https://htaccess.madewithlove.be/
This will let you have a test input URL and see if you get the proper output URL (the actual URL your server would read).
With that being said, I would recommend you to look into routing for future projects and self-development. A framework I'd recommend checking out is fat-free framework (f3) for PHP. It's very easy to get into, you could learn it in 2 days, and it saves a lot of time in development - by containing routing, ORM, global variables, simplified SQL queries, and plenty of other features that essentially keep you a PHP developer, rather than a framework developer.
use below method:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/([0-9a-zA-Z_=-]+)$ blog-details.php?title=$1 [NC]
</IfModule>
Then you can access the title like $_GET['title'] on the page.
Using the following htaccess, I have been able to rewrite example.com/profile.php?username=xyz to example.com/xyz,
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
Adding the following to the above,
RewriteRule ^([a-zA-Z0-9_-]+)/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/section/$ section.php?user=$1
did not resolve example.com/section.php?username=xyz to example.com/xyz/section.
Am I doing something wrong here?
First of all: The manner of speaking would rather be the opposite: The rules you showed are to rewrite requests like /xyz internally to /profile.php?username=xyz and not vice versa.
Now if you want to rewrite requests like /xyz/section internally to /section.php?username=xyz where section and xyz are variable, try these rules:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([^/]+)/?$ $2.php?user=$1
To look for static files (images, css) in the right directory without having to write the file address, do:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Before writing the code suggested by RageD
(Sorry, should have posted it as a comment but I needed newlines)
I tested it and it works. Try this:
RewriteRule ^([a-zA-Z0-9_-]+)\/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)\/section\/$ section.php?user=$1
All I did was escaping the / in the URL since / is a regex delimiter.
I need help configuring my .htaccess file to handle redirects properly.
Here’s what I need to have happen. Stackoverflow's spam filter wouldn't allow me to post the full domain. So where I say "DOMAIN" you can substitue "domain.com". (I also needed to add and extra t to the http.)
Requests for the DOMAIN/page version of the file should be redirected to www.DOMAIN/page.
Requests for the 'friendly' versions of the URLS should be allowed. So a file that is really at www.DOMAIN/index.php?q=37 should be viewable by going to www.DOMAIN/latest-news
I have a big list of 301 redirects. We recently changed the site from an .asp based CMS to one written in PHP.
Example:
redirect 301 /overview.asp http://www.DOMAIN/overview
Items 1 and 2 are working fine.
However for item 3, if I put in a browser request for "http://www.DOMAIN/overview.asp" instead of redirecting to the friendly name of the file ("http://www.DOMAIN/overview") it will redirect to http://www.DOMAIN/index.php?q=overview.asp. This is the problem.
What do I need to change to get this working right?
My configuration is below:
## Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]
## Exclude /assets and /manager directories and images from rewrite rules
RewriteRule ^(manager|assets)/*$ - [L]
RewriteRule \.(jpg|jpeg|png|gif|ico)$ - [L]
## For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.DOMAIN/$1 [R=301,L]
redirect 301 /overview.asp http://www.DOMAIN/overview
redirect 301 /news.asp http://www.DOMAIN/news
# ETC....
thanks!
Mod_rewrite is doing exactly what you're asking it to do ... (yes :-), that's often the problem with computers).
On the /overview.asp http://www.DOMAIN/overview line you're setting the browser to send out a brand new request from scratch, which starts the whole cycle again from the top and gets catched by the ^(.*)$ index.php?q=$1 directive.
Right before this line you should put another RewriteCond to prevent the ^(.*)$ rule to apply if REQUEST_FILENAME is either overview or news. You might also simply rewrite /overview.asp to overview [L] instead of redirecting.
If you can, set the RewriteLog directive to its highest verbosity and look at the logfile - it usually gives very good insights into what's really going on...
EDIT - if I get it right you shoud be doing this:
RewriteCond %{REQUEST_FILENAME} ! \.asp$
RewriteCond %{REQUEST_FILENAME} ! ^overview$
RewriteCond %{REQUEST_FILENAME} ! ^news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This would prevent any file already ending in .asp, plus those looking for overview and news, to be redirected toward index.php.
I suspect anyway that you got something backwards regarding that SEO stuff. You should indeed start from the structure of the query string that your scripts expect and use that as a base to build a sensible URL addressing schema.
EDIT #2:
There was a space too many between the bang mark ant the regex. The following code doesn't come from memory as the previous - I've tested on my local Apache and it does what's supposed to do (as long as I've understood correctly..)
RewriteCond %{REQUEST_FILENAME} !\.asp$
RewriteCond %{REQUEST_FILENAME} !overview$
RewriteCond %{REQUEST_FILENAME} !news$
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Hope this helps
I currently use the follwoing code in my .htaccess file
RewriteEngine On
RewriteBase /
# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
With that code, every time I type for instance, /en at the end of the URL, it redirects me to /?lang=en (loads the the English content from a PHP array):
For instance:
example/en redirects me to example/?lang=en while keeping example/en in the URL.
But I also have a thanks.php page and the code above clearly just work for the index.php page.
How can I make the rewrite rule to work for both index.php and thanks.php page?
The most straight-forward way is just to do this:
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
RewriteRule ^thanks/(en|es|zh\-tw|zh\-cn)/?$ thanks.php?lang=$1 [L]
If you want to make it more general, you have the option of white-listing files, like this:
RewriteCond $1 ^(thanks)/$ [OR]
RewriteCond index ^(index)$
RewriteRule ^(.+/)?(en|es|zh\-tw|zh\-cn)/?$ %1.php?lang=$2 [L]
...where (thanks) would be a pipe-delimited list of the files you wanted to have this functionality, or you can just accept every request as a pass-through to an existing PHP page:
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?[^/]?)/(en|es|zh\-tw|zh\-cn)/?$ $1.php?lang=$2 [L]
Hey, I've been reading StackOverflow.com for a long time but decided to sign up to ask a question. I'm writing my own lightweight MVC framework that routes page requests in index.php.
Page requests look like /controller/action/arg1/arg2/arg3, and they should be rewritten to index.php?route=[request]. So, a [request] like site.com/user/profile/123 should be rewritten to index.php?route=user/profile/123
However, files aren't meant to rewrite to index.php. Assets such as images and stylesheets are in the /app/webroot/ folder, and don't need PHP to be executed. So, the mod_rewrite engine should rewrite any filerequests to /app/webroot/, and serve the configured 404 ErrorDocument when the file doesn't exist.
Directory structure
./index.php
./app/webroot/scripts/helpers/hamster.js
./app/webroot/images/logo.png
./app/webroot/style/main.css
Since you can tell the difference between a file request (/squirrel.png) and a page request (/user/profile/123) just by the existence of the file extension / dot, I was expecting that this would be really easy. But... I'm having a really hard time with it and I was hoping someone could help me out.
Something I've tried was...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app/webroot/$1 [L]
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
... but it doesn't really work except for redirecting correctly to existing files. Pagerequests or nonexisting files result in HTTP 500 errors.
Any help is greatly appreciated! =)
See if this works out a little more like you expected:
RewriteEngine On
# These two lines are very specific to your current setup, to prevent
# mod_dir from doing what it does, but in a more controlled way
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/iceberg[^/]
RewriteRule .* http://localhost/iceberg/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/app/webroot
RewriteCond %{REQUEST_URI} \.[a-z]+$ [NC]
RewriteRule ^.*$ app/webroot/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/app/webroot
RewriteRule ^.*$ index.php?route=$0 [QSA,L]
Also, to explain, the reason why you are getting the 500 error is likely because of your rule:
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
Since it's unconditional, and the regular expression pattern will always match, your rewrite will be performed over and over (the L flag doesn't prevent this, because after you rewrite to index.php, an internal redirection is made inside of Apache, and the process loses its current state).