Simple all in one mod_rewrite for static prototyping - php

I will provide a little background of my problem. I'm working on a static HTML5/CSS3 prototype for a PHP CMS. One of the things I want to is to have clean URLs. I had stuff like this at the start:
localhost:8888/file.php
And wanted it to:
localhost:8888/file or without the trail slash localhost:8888/file
I did a quick search on Stackoverflow and found several results over the matter and I kindly used this one to do the job:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*?)/?$ $1.php
RewriteRule ^(.*?)/?$ $1.php
The problem with this approach is when i want to set deep levels on my CMS structure, like site/posts. Without mod_rewrite I need to create folders and create intense changes over the code to make things work like ../images/ to ../../images/ and so on, so boo to that solution. I need to go with mod_rewrite to make it work.
In addition, that mod_rewrite chunk of code doesn't work either with the folder stuff so I started a search in here to find a solution so I can do /level1_level2_levelx.php to /level1/level2/level3/ or /level1/level2/level3. I did search in many ways but i don't find the correct answer to this.
Thank you all,
PD: I'm using MAMP.

On the Rewrite front I'm guessing the the following should do what you ask:
Internally rewrite entered URL:
http://localhost:8888/file
OR:
http://localhost:8888/file/
to:
http://localhost:8888/file.php
Rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteCond %{REQUEST_FILENAME} ^(.*)/?$
RewriteCond %1\.php -f
RewriteRule ^(.*)/?$ $1.php [L]
But sound like you really want a custom 404 handler configured in Apache e.g.
<FilesMatch "\.(css|js|jpg|png|gif)$">
ErrorDocument 404 "File not found."
</FilesMatch>
<Files favicon.ico>
ErrorDocument 404 "-"
</Files>
ErrorDocument 404 /404-handler-script.php
And an associated 404-handler-script.php added to do whatever mapping you want internally e.g.
http://www.phpriot.com/articles/search-engine-urls/4
Note: This script redirects the browser to the destinations script rather than serve it itself.

Related

RewriteRule to redirect to php file not working

I just want a simple redirect to clean up the url's on a site.
e.g.
I want ajhtestserver.com/registration/ to redirect to ajhtestserver.com/registration.php
It should be easy and I have successfully used .htaccess rewrites on other sites but for some reason it just will not work for me today.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^registration[/]$ registration.php [NC,L] # Handle requests for "registration"
I am sure it is something simple that I am missing but I basically just copied what I have on other sites that work fine for me so I am confused as to why it just refuses to work for me here (gives me The requested URL /ajhtestserver/registration/ was not found on this server. error). Just one of those days :(
Any help is appreciated.
Thanks,
Adam
if you use apache ,first you should enable rewrite_mode in http.conf or ...\
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^registration/(.*)$ registration.php/$1 [L]
check .htaccess syntax or rewrite mode.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)[/]$ $1.php [L]
Well it didn't seem to like it when the redirect source word and target filename were the same word but this works...
RewriteRule ^([a-zA-Z\ ]+)[/]?$ $1.php [NC,L]
And that is actually a better solution anyway as it doesn't require a separate rule for each page.
Though I never did figure out why it didn't like it the original way.

phpDolphin SEO Urls

I'm currently using phpDolphin as my website social network, phpDolphin is not SEO URL ready, I was researching on how to fix the .htaccess for friendly seo urls and I cannot seem to deal with it. Here's my current .htaccess
Also here's how urls appear
http://feisbu.me/index.php?a=profile&u=cubaton3
and I want to show: http://feisbu.me/cubaton3
Here's my .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Please help me, thanks.
A few obvious things:
Mod_rewrite does not change the links you output. Your server side script needs to do that.
Your conditions and rules are kind of a mess, and you clearly need to hardwire your a=profile parameter. Remember that you're matching on, and rewriting the query string unless you specifically are using hostname variables, but they should not be relevant to matching, and are not part of the rewriting process unless you're issuing 301's.
This is typically done with something like this:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l
RewriteRule ^/(.*)$ /index.php?a=profile&q=$1 [L,QSA]
I'm not familiar with phpDolphin or other routes that this might interfere with, so you will probably need other rules. I personally am not a fan of these "if nothing default route to users" type of setups. Something like "/user/name" would be a cleaner route that will not interfere with other routes, since you can exact match on the parameters.
This is the solution, hope I helped, my site is gofinder.org:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?a=profile&u=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]

.htaccess mod_rewrite for pretty urls not working

Trying to get www.example.com/test.php?archives=testing to www.example.com/archives/testing
According to godaddy they have mod_rewrite enabled by default
Here is my .htaccess file:
rewriteengine on
rewritecond %{HTTP_HOST} ^example.com$
rewriterule ^example\/(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
However this is not working, when i go to www.example.com/archives/test I get a 404, suggestions
I just left this in a comment but i might as well put it here so its seen easier. I wrote an answer for someone thats helped others out over time, and in the end this isn't exactly an answer to what your asking however its more of a stepping stone in the direction. The original question was asked how to work with short url strings and make them work in a fashion like your looking for, but rather copy and paste that answer here. Ill let you go there and read over it.
Its not to go without saying you will need to alter the rule a little for your specific needs but it will in the end serve its purpose for getting you where you want to be.
PHP dynamic DB page rewrite URL
You need some rewrite conditions to specify when this rule will be used. Without them, you will keep running the same rewrite rule indefinitely, giving you an error. Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
How about this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(.+)$ /test.php?archives=$1 [L]
</IfModule>
Without is being super clear what you are trying to get from your rewrites I suggest:
Options -MultiViews
ErrorDocument 404 default
RewriteBase /

.htaccess not allowing sub-directory to work

Here is the .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.html|index.php|administrator|system|template|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.html?tpl=$1 [L]
Options +FollowSymlinks
However what is happening is that all the following folders wont allow me to go into them and view a PHP file.
|administrator|system|template|js|lib
I thought by putting the files in RewriteCond that it would allow a user to still go to http://example.com/news/happy which then would go to the index.php?tpl=etc etc but it has stopped me from going into any subdirectory like
http://example.com/system/index.php
I don't know if this would work for you. But what I do is to add this rules to load calls to any .php file without the .php ending. You can call both, with or without that ending.
For example, you can call my_url.com/myfolder/myfile and it will load myfile.php, with the friendly URL.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
i would like to recommend that you read about mod rewrite first
1 - put that line at top
Options +FollowSymlinks
That line ask your server to follow symlinks but i would like recommend that you use the more secure option "SymLinksIfOwnerMatch"
Source:
http://httpd.apache.org/docs/2.2/mod/core.html#options#FollowSymLinks
RewriteEngine on
Enables or disables runtime rewriting engin
Source:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteengine
RewriteCond $1 !^(index\.html|index.php|administrator|system|template|js|lib|favicon\.ico|robots\.txt)
Here you go . that rule ask the web server to add rewrite Condition to ignore everything listed in your regex
also i have added regex explain for you
!
not equal
^
Start with
RewriteRule ^(.*)$ /index.html?tpl=$1 [L]
you are asking to write everything else to index.html?tpl=$1
** I think you mean index.php i will assume that you don't need the query string
i would like to recommend using a better way to handle the excluding
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(index\.html|index.php|administrator|system|template|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.html?tpl=$1 [L]

help with setup of .htaccess file redirects

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

Categories