I'm using url rewriting in my website.
I've these url:
works
example.com/_new/app/planning
example.com/_new/app/user_info
doesn't work (404)
example.com/_new/try
example.com/_new/features
Here my rules:
RewriteEngine On
# Removing extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# example.com/app/[module]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)$ app.php?param1=$1 [L]
# example.com/app/[module]/[action]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)$ app.php?param1=$1¶m2=$2 [L]
# example.com/app/[module]/[action]/[type]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1¶m2=$2¶m3=$3 [L]
# example.com/app/[module]/[action]/[type]/[id]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1¶m2=$2¶m3=$3¶m4=$4 [L]
Here the file structure:
/_new/
/_new/try.php
/_new/login.php
/_new/app.php
The try.php and index.php should be shown as example.com/try or example.com/login.
The app.php loads module from another directory. This one should be:
example.com/app/module_name.
What's the problem ?
Thanks.
In htaccess, the base URL example.com will be skipped in the matching, and in your file, the first regex ([^\.]+) will match /_new/try and it tries to redirect you to a file /_new/try.php which does not exist, hence 404.
Make sure to test your regex before trying to use the URL, you can benefit from https://regex101.com/ to make your tests.
Related
I'm trying to rewrite my URL from request/2 to request?number=2
I tried to write this in my .htaccess
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/(.*)$ request?number=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But the result is a 404 Error
Can someone help me?
With your shown samples, could you please try following. Please do clear your browser cache before testing your URLs.
RewriteEngine ON
##To remove .php extension eg--> someone hits http://localhost:80/test.php it will redirect to http://localhost:80/test
RewriteCond %{THE_REQUEST} \s([^.]*)\.php\s [NC]
RewriteRule ^ %1 [R=301]
#RewriteRule ^(.*)$ $1.php [L]
##All non-existing files/directories will come in this rule whose URI starts from request.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/([\w-]+)/?$ request.php?number=$1 [NC,L,QSA]
##All non-existing files/directories will come in this rule set apart from URI which starts from request which is taken care above.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
the above i have in my htacces file which rewrites directory/page to index.php?id=directory/page
thats working fine.
I also want to be able to add the following to it:
domain.com/sections/page rewrites to index.php?section=1&id=page
domain.com/sections/page2 rewrites to index.php?section=1&id=page2
domain.com/page rewrites to index.php?id=page
the ID is going to be different for each page
You have to take a look at RewriteCond and RewriteRule directives.
That's a sample .htaccess based on your edit.
RewriteEngine On
# This will process the /sections/(.*) requests, ?section=1 will be appended to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sections\/(.*)?$ index.php?section=1&id=$1 [L,QSA]
# This will process the other requests, as it does now.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
I'm using a WAMP installation and routing everything through my index.php file. The CSS and JS files in my header get called just fine when I remove the .htaccess file, so the file paths are correct, but once I insert the .htaccess file again Chrometools throws this error upon including my JS file
Uncaught SyntaxError: Unexpected token <
When viewing Chrometool's Network>>Response, it shows the JS file contains the content of everything rendered by the index.php file.
Here's my code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteRule ^([^/]*)$ index.php?_page=$1
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
RewriteRule ^([^/]*)/([^/?]*)/(.*)$ index.php?_page=$1&_action=$2&$3 [QSA,L]
I thought the %{REQUEST_FILENAME} !-f was supposed to prevent any rewriting of a file when it's called directly?
UPDATE
I've found the line causing the issue is is the second RewriteRule.
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
Because my JS and CSS are stored two directories deep from my project root, the rule is directing the file to my index.php.
Neither of these below commands have any affect until I comment out the RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
Any ideas?
Try and change this line.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
To this
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
See of that works for you.
Edit:
You need the conditions for all your rules. Rewrite Conditions only applies to the first rule following it. Also you can just have it so if it matches static content, then just not do anything. Try your rules this way.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
RewriteRule ^(.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?_page=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/?]*)/(.*)$ index.php?_page=$1&_action=$2&$3 [QSA,L]
I'm using the following rule to serve the associate .php file
All last directory pathname will go to the associate .php file
Ex. /about/ or /about will go /about.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php`
So when I use http://example.com/abc it points to /abc.php, this is working fine. But if /abc.php file does not exist in that directory then I like to forward that to /listing.php file. How can I do that? please help
Also, I need this to apply to the root directory only. means
http://example.com/..*here*.. not for any subdirectory.
Thanks
Try this, this should be tested:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0\.php !-f //check if (for example) abc.php not exists then
RewriteRule ^(.*)$ /listing.php
RewriteRule ^(.*)$ $1.php
Update: I tested this on my local server, note that: the folder is test you can adjust for your own
Options All -Indexes
RewriteEngine on
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond test\/$0\.php !-f
RewriteRule ^(.*)$ listening.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
When a page is accessed via an URL like this:
/pagename
How can we check if the following file exists via htaccess
/pagename.htm
And then load index.php if not?
Here's what I'm working with:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I've tried a variation of this but no luck:
RewriteCond %{REQUEST_URI} !/.htm -f
# If the URI is not .htm and doesn't have any additonal params, try .htm
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?![^/]+\.htm)([^/]+)$ $1.htm [L,R=302]
# Adding the R flag causes the URI to be rewritten in the browser rather than just internally
# If the URI is .htm but it doesn't exist, pass to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+\.htm)$ $1/no [L]
# Passes requested path as 'q' to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/no/?$ index.php?q=$1 [L]
Should work rather elegantly for you. If it can't find the file, it just passes it on to index.php. You can of course modify what variable it uses, I just used 'q' for generic purposes.
Also, do note that you can condense this down to a single set of RewriteConds. I've expanded them for annotation purposes here.