I have problem with alias on URL.
.htaccess file
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
I try use with config
'<controller:\w+>/<action:\w+>/<alias:\w+>'=>'controller/action',
but URL still access with link
controller/detail?alias=abc-xyz
Not access link
controller/detail/abc-xyz
I don't understand why.
please help me.
Thanks,
Regex shorthand character \w matches [A-Za-z0-9_] so as you see there is no single - here (well, there is but for ranges only which is different thing). Read more about regex at www.regular-expressions.info.
Modify your rule like this:
'<controller:\w+>/<action:\w+>/<alias:[\w\-]+>'=>'controller/action',
Related
example.com/play/music to example.com/play.php?c=music
here is my code which works:
RewriteRule ^play\/music$ play.php?c=music [L]
now I have an extra url param (id as integer) added:
example.com/play/music/54
need to be - example.com/play.php?c=music&id=54
here is my beginning try:
RewriteRule ^play\/music/\d+/?$ play.php?c=music&id=$1 [L]
the page is there but problem is with id param - php doesn't see it
could someone help and explain ?
also, maybe is possible to join both rules (with and without id) in one ?
With your shown samples, please try following .htaccess rules file. Make sure to clear your browser cache before testing your URLs and make sure your .php files(eg: play.php etc) are residing along with .htaccess rules file.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1.php?c=$2&id=$3 [QSA,L]
I am trying to write a .htaccess code which should work in root directory as well as children directories.
This is how sample requests will look like: /z /a/z /a/b/z
For each case, I want to check if these respective file (/m/z, /a/m/z, /a/b/m/z) exists using RewriteCond . if they exist, follow this RewriteRule rule RewriteRule .* code.php?alias=$0 [QSA].
I am unable to write this special RewriteCond for this case and need help with it. Note I cannot used RewriteBase.
I finally am able to find a solution which seems to work.
RewriteCond %{REQUEST_URI} ^(/.*)?/(.*)$
RewriteCond %{DOCUMENT_ROOT}%1/m/%2 -f
RewriteRule .* %1/code.php?alias=%2
I am making a website builder an I would like to make urls prettier.
The url would be for example:
https://ccc-bb.example.com => https://example.com/project/show/ccc/bb
This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# prevents files starting with dot to be viewed by browser
RewriteRule /\.|^\.(?!well-known/) - [F]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\-(.*)$ https://example.com/project/show/$1/$2 [L]
When I use above (https://ccc-bb.example.com) it sends me to the subdomain empty folder. The folder has only the .htaccess file.
What am I missing? I've never edited an .htaccess file and Google didn't help me (or I don't know what should I looking for).
Your first rule for dotfiles is okay but would be better the other way around, since the second part can only match the start, but the first can only match in subdirectories.
RewriteRule ^\.(?!well-known/)|/\. - [F]
Your other rule's problem is that you are expecting it to match the subdomain. RewriteRules do not operate on the entire string you see in your browser's address bar, only the path part, and in .htaccess they see even less as the leading directory is stripped off, too. To access the info you want, use a RewriteCond:
RewriteCond %{HTTP_HOST} ^([^-]++)-([^-.]++)\.example\.com$
RewriteRule ^(?!project/show/).* project/show/%1/%2/$0 [L,DPI]
(You don't need to include \.example\.com$ if your main domain contains no hyphens.)
I am trying to make this:
http://www.testsite.com/bacon
be this
http://www.testsite.com/index.php?food=bacon
so the user types in/clicks on a link that is http://www.testsite.com/bacon but the server interprets it as http://www.testsite.com/index.php?food=bacon.
Some explanation to any solutions would be valuable too.
You can use this code in your .htaccess file to make a vanity URL for your task
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ index.php?food=$1
Explanation
Our first line turns on the rewrite engine.
The line above checks to see if the request is a file
This line turns adds the .php extension. So for example if you URL is /friends, the friends.php file would be called.(So this part is optional according to your use case)
The final two lines pick up the url when it is not a file name, for example, your-url.com/philipbrown and routes it to profile.php?user=philipbrown.
Visit this link for more information
I have the following lines in the .htaccess file in the site directory:
RewriteEngine On
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
If it receives URL for example :
http://localhost/samplesite/johnsmith
it will rewrite it to
http://localhost/samplesite/account.php?username=johnsmith
which is fine.
The problem occurs when there is a directory named johnsmith in the site directory. then the URL is rewritten to and displayed as
http://localhost/samplesite/johnsmith/?username=johnsmith
and that is a problem. I am trying to implement account pages functionality for every user but if a user wants to register a username like some directory in the root the functionality will break? I tried adding rewrite conditions to check if the requested URL stands for an existing directory or a file :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
but I don't know how to proceed.
If someone knows a better way to do account pages functionality for users I would appreciate to give me a piece of advice on that.
Can anybody help me solve this case? Thank you!
Your RewriteCond will work if it is in the correct place. Your full .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# ---------------------------------------------------------------See info below--
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
Also, you don't need the %{QUERY_STRING}. Instead you should use the QSA flag to append the rest of the query string...
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1 [L,QSA]