htaccess and turning ?page into /(whatthepageis) - .htaccess

Ok, so I am wondering what is the correct way to do .htaccess so that a url like this
http://www.mysite.com/?page=spinme
looks like this
http://www.mysite.com/spinme
It still needs to load index.php
but I am not sure how to do it, I know sounds simple.
Also I need it not to effect images or style sheets that are already
http://www.mysite.com/(what ever the folder link)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f //not to effect images or style sheets
RewriteCond %{REQUEST_FILENAME} !-d //not to effect directory's
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

To make
http://www.mysite.com/?page=spinme
to look like
http://www.mysite.com/spinme
You first of all need to make each output of that URI to be
http://www.mysite.com/spinme
That step is very important (at least for the display, in terms of working, you can skip this step and your site will just work).
If you've managed that, in your .htaccess file, just add a single rule:
RewriteRule ^spinme$ index.php?page=spinme [L]
and you're done.
If you're concerned about Search Engine Optimization (SEO) you should use this URI instead:
http://www.mysite.com/?page=spinme
It will show the search engine that your site has pages and they have a name. That's additional information searchengines process nowadays, comparable to keywords in earlier days.
Hope this is helpful.

Related

Multiple rewrite conditions for multiple parameters

There is an issue with the htaccess rewrite conditions in my setup.
Currently I have the following code.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://mydom.com/$1.php
This works fine for any base page making them look like this.
http://mydom.com/page
What I want to also be able to do is add parameters from the url if they exist. I have some pages that will be like this.
http://mydom.com/page?param=1&secondParam=2
What I've tried to do is add this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ http://mydom/$1/$2/$3 [L]
RewriteRule ^(.*)/(.*)$ http://mydom/$1/$2 [L]
This made sense to me, because I thought if the condition didn't match, it would move on, but this gave me an internal server error.
What I ended up doing was setting up a separate rule for each page that could have multiple parameters like this.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.)/(.)$ http://mydom.com/page.php?param=$1&secondParam=$2 [L]
RewriteRule ^page/(.*)$ http://mydom.com/page.php?param=$1 [L]
RewriteRule ^(.*)$ http://mydom.com/$1.php
This works, however you need to keep in mind relative links you may have in your site such as style sheets and javascript files. In my case, I had to replace all relative paths with full site paths, depending on the way you set up your site, it could take a while to replace.

Plus sign in htacesss rewrite for users

I want to be able to create the url: http://example.com/+/user
Is it possible using .htacess rewrite.
I have searched the web and have come across nothing relevant to what I want.
You have to use really simple RewriteRule...
RewriteRule ^\+/user$ /test.php [PT,L,QSA,NC]
Or if you want to "catch" username from address, you have to use something like this
RewriteRule ^\+/(.+)$ /test.php?username=$1 [PT,L,QSA,NC]
With whatever rewrite_mod options you need.
Not so much things to do, simple things are often better ones :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\+/(.+)$ index.php?user=$1 [L]

Possible .htaccess errors, 1and1.com not rewriting

The following .htaccess file works perfectly on my local server.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.cssc
RewriteRule . style.php [L]
RewriteRule ^admin\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . admin.php [L]
I am doing some work for a client and he is using 1and1.com. I do not know anything about his account or what package he has.
All files are rewritten to admin.php (unless they actually exist). The problem is, I am getting a 404.
I know it's reading the .htaccess file because:
I can put garbage in the file and get a 500 error.
If I do a general rewrite (all pages go to admin.php), it works.
Also, it seems that 1and1 does it's own rewriting. If I go to: http://somewhere/afile, it will include afile.js even though I am not requesting the .js.It is super strange.
Does anyone have any experience with this? Or any insight?
To add some information..
1and1 seem to have some sort of default rewriting already in place that can interfere with your own rules - for example:
RewriteRule ^product$ /product.php [L]
It gave a 404 for this, and also for any other rewrite rule that matched a pre-existing .php file in my root folder.
2 things that 'fixed' it:
Change rewrite text to no longer match filename:
RewriteRule ^productz$ /product.php [L]
Or, change filename:
RewriteRule ^product$ /product_.php [L]
Both work - both very annoying.
The answer was simply "Do not nest .htaccess files". I had one in a sub folder.

Excluding a script from the general UrlRewrite rules

I have following rewrite rules for a website:
RewriteEngine On
# Stop reading config files
RewriteCond %{REQUEST_FILENAME} .*/web.config$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .*/\.htaccess$ [NC]
RewriteRule ^(.+)$ - [F]
# Rewrite to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(/bilder_losning/|/bilder/|/gfx/|/js/|/css/|/doc/).*
RewriteRule ^(.+)$ index.cfm?smartLinkKey=%{REQUEST_URI} [L]
Now I have to exclude a script including its eventually querystrings from the above rules, so that I can access and execute it on the normal way, at the moment the whole url is being ignored and forwarded to the index page.
I need to have access to the script shoplink.cfm in the root which takes variables tduid and url (shoplink.cfm?tduid=1&url=)
I have tried to resolve it using this:
# maybe?:
RewriteRule !(^/shoplink.cfm [QSA]
but to be honest, I have not much of a clue of urlrewriting and have no idea what I am supposed to write. I just know that above will generate a nice 500 error.
I have been looking around a lot on stackoverflow and other websites on the same subject, but all I see is people trying to exclude directories, not files. In the worst case I could add the script to a seperate directory and exclude the directory from the rewriterules, but rather not since the script should really remain in the root.
Just also tried:
RewriteRule ^/shoplink.cfm$ $0 [L]
but that didn't do anything either.
Anyone who can help me out on this subject?
Thanks in advance.
Steven Esser
ColdFusion programmer
Please try to put the following line at the top of your config (after RewriteEngine on):
RewriteRule ^shoplink.cfm$ - [L]

mode_rewrite: example.com/münster -> example.com/index.html?city=münster

Like described in the title i would like to have clean URLs with mod_rewrite, sadly i didnt manage to write the fitting regex to get the job done i am experimenting with:
1.try:
RewriteRule ^([a-z]*)$ /index.html?city=$1 [NC,L]
2.try:
RewriteRule ^(.*)$ /index.html?city=$1 [NC,L]
But none is working properly, even less when it comes to "üöä"
The second one would be better, however, it'll go in a loop as the redirect to /index.html?city=$1 will be processed as well, and as it matches the RewriteRule it will also be redirected. Something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html?city=$1 [NC,L]
will work better as it ensures it only redirects requests that aren't for existing files or directories. I tried the "münster" example with the above and it redirected as /index.html?city=m%c3%bcnster which may or may not be what you require.

Resources