PDA

View Full Version : Rewrite Rule: Filename Without a Comma


robj
06-05-2009, 09:23 PM
I'm attempting to rewrite the following:
/test,name.php to /test

Here's my current .htaccess file:

RewriteEngine On
RewriteBase /

# Externally redirect direct client requests for .php files to non-.php URLs
RewriteCond %{THE_REQUEST} ^GET\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.MYURL.com/$1 [R=301,L]
#
# Internally rewrite extensionless page URLs to PHP files
# if no extension or trailing slash on requested URL
RewriteCond %{REQUEST_URI} !(\.¦/$)
# and if filename exists when .php is appended
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.+) /$1.php [L]


I've tried quite a few attempts without getting close. I'm not as skilled with apache as I'd like (hence the notes) The above works perfect, but I need help with remove the comma and everything past it.

Thanks

schleppel
06-06-2009, 08:38 PM
I'm attempting to rewrite the following:
/test,name.php to /test
The browser's address bar will contain /test and the file the user should see is /test,name.php? Does "name" change?

To just remove the .php extension, try
Options +FollowSymLinks

RewriteEngine On

# Remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\ [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [R=301,L]

# Remove .php
# Only match "browser" requests (not internal Apache/mod_rewrite requests).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Make sure the PHP file exists.
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# Add .php back.
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /$1.php [QSA,L]

robj
06-08-2009, 05:03 PM
The browser's address bar will contain /test and the file the user should see is /test,name.php? Does "name" change?
Yes, to both of these.

The above mentioned does not work. Here's the error:
You don't have permission to access /test/ on this server.

The idea is to separate the filename into 2 variables. Comma separated.
There another custom program that generates these filenames, saves them to a folder.
If there are issues with the common as a separator, the comma can be changed to another character (underscore, dash, a period etc.).

schleppel
06-09-2009, 12:54 AM
Yes, to both of these.
If both test and name change they both need to be in the new URL.

There another custom program that generates these filenames, saves them to a folder.
You are going to create /test,name.php with a script?

The above mentioned does not work. Here's the error:
You don't have permission to access /test/ on this server.
Do you have any files/directories with test in the name? Try creating a totally random named PHP file.

robj
06-09-2009, 05:33 PM
Here's the short story:

The files are being generated by a different department and are automatically placed on the server. There are 2 parts to the file name. The first is the type of data (which will always be different), and the second is the time/date/version/revision#/ stamp. The parts are currently being separated by a comma, but could be changed to a hyphen or period (underscore is cannot be used unless it's possible to define the last underscore as the separator).

I only want to display the first part of the file name, and preferably drop the .php extension. Example: http://www.MYURL.com/first_name,123456789.php would read http://www.MYURL.com/first_name

Is it require to keep both parts in the new URL?

I appreciate your help!

schleppel
06-10-2009, 01:12 AM
The separator does not matter.

I only want to display the first part of the file name, and preferably drop the .php extension. Example: http://www.MYURL.com/first_name,123456789.php would read http://www.MYURL.com/first_name

Is it require to keep both parts in the new URL?
Will first_name be unique? If not how would it know which one to send.

If first_name is unique, you could use a RewriteMap (http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap) (if you have access to the httpd.conf file) with a text file (first_name filename pairs) or program or you could pass the requests to a PHP script that could include the PHP file once it works out the correct one). But you can't do it with mod_rewrite only.