I would like to remap index.php file as a directory..
Im real url, it is simply: localhost/app/index.php?app=my-app
I am new in mod_write and I know how it really works. I tried to use this:
RewriteRule ^app/(.*)/?$ index.php?app=$1 [QSA]
And it only works with localhost/app/my-app.. now how can I make it work on with or without slashes at the end and also with a filename index.php..
I want something like this:
localhost/app/my-app
localhost/app/my-app/
localhost/app/my-app/index.php (if other filename then error)
RewriteRule ^app/([^/]*)(?:|/|/index\.php)$ index.php?app=$1 [QSA]
See Regex101.
EDIT: Fixes (see comments)
Related
I'm trying to write clean url's for my website. I'm a rookie at this so forgive me. My .htaccess file currently looks like this:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)$ something.php?query=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ something.php?query=$1
The first rule seems to work. For example website.com/good-looking-query-string does in fact rewrite to website.com/something.php?query=ugly+looking+query+string
The second rule is where I'm having a problem. I can't get that trailing slash to work. For example website.com/good-looking-query-string/ appears to pull up the page but without any CSS rules applied. I noticed all the links end up appended to the query string also. For example the link back to index.php ends up like website.com/good-looking-query-string/index.php
I need to get that trailing slash to work. What in the world am I doing wrong?
You can combine two line in one line:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1
In this case / will be optional
Your issue is client side, you are using relative paths but you need to be using Root-relative paths. A URL of:
website.com/good-looking-query-string
loads from the root. A url of:
website.com/good-looking-query-string/
loads from the good-looking-query-string directory.
So instead of href="style.css" you should have "href="/style.css"and the index issue should behref="/index.php"instead ofhref="index.php"`.
As noted in the earlier comment your regex could be simplified by adding a ? on the trailing slash to make it option. That is not your issue though, just simplifies the rules.
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1
I already create a .htaccess in my folder and would like to make the URL:
www.example/good/page
toward:
www.example/page?type=good
my current document is written as:
RewriteRule ^(\w+)$\/page page.php?type=$1 [NC,L]
but it didn't work, and I don't know how to check it is correct or not
would anyone able to provide some example for that?
thanks!
Make sure your rewite engine is on, and it is rewriting the base. Change your code like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/?([^/]+)/page?$ "page.php?type=$1" [L,QSA]
And then your rewrite rule must look like this.
The RewriteRule basically means that if the request is done that matches ^/?([^/]+)/page?$ (matches any URL except the server root), it will be rewritten as page.php?type=$1 which means a request for page.php be rewritten as page.php?type=good).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (example.com/good/page?id=2 will be rewritten as page.php?type=good&id=2.
L means if the rule matches, don't process any more RewriteRules below this one.
Try this and let me know. Accept the answer if it worked for you.
I know there are tons of questions already but looks like i'm missing a step.
I recently added a login/signup form to my website. Users can then fill out a quick bio on their profile.
The issue that i'm facing is that right now, the profile page URL looks like that:
"www.example.com/profile.php"
How can I edit the .htaccess file so that it looks like:
"www.example.com/neil/
(neil being the username)
I've tried to add this to my .htaccess file but it didn't work.
RewriteEngine On
RewriteRule ^/(.*)/$ http://www.example.com/?name=$1 [L]
fiy, i'm working on localhost.
Just to be clear: my htaccess file should have no extension right (should't end with .txt) ?
Thanks
I suggest the following:
RewriteRule ^(.+)/$ profile.php?name=$1 [L]
The rule (.+)/ matches a string of one or more characters that ends with a slash, but returns the string without the end slash.
Changes this: http://www.example.com/neil/
To this: http://www.example.com/profile.php?name=neil
Changes this: http://www.example.com/ne/il/
To this: http://www.example.com/profile.php?name=ne/il
EDIT:
As suggested by sgroves:
Since names will probably not contain slashes, you can only match strings that don't contain slashes like this:
RewriteRule ^([^/]+)/$ profile.php?name=$1 [L]
I have a URL that is like the following:
http://www.example.com/client/project/subdirectory/value/
I would like like a simple way to be able to change/redirect the URL to the following:
http://www.example.com/client/project/#/subdirectory/value/
Once the redirect is complete, the hash needs to be accessible via JavaScript. I'm okay with a full refresh/redirect, just ideally that I write this once and don't have to change it again.
In other words, when the site goes live, the URLs will be structured differently, so that:
http://www.example.com/subdirectory/value/
Will change to:
http://www.example.com/#/subdirectory/value/
Edit:
I have tried using this:
RewriteRule ^profile/?$ #/profile/ [ NC,L]
Which doesn't seem to do anything
Also tried this:
RewriteRule ^profile/?$ /#/profile/ [NC,L]
Which takes me to the root directory
Also tried this:
RewriteRule ^profile/?$ #/profile/ [R,NC,L]
Which adds the whole root path to the server, followed by /%23/profile/
If you have a URL like the following:
http://localhost/tests/redir/subdirectory/value/
And you want to get it redirected to:
http://localhost/tests/redir/#/subdirectory/value/
Place a .htaccess file into the directory of tests/redir with the following:
RewriteEngine On
RewriteBase /tests/redir
RewriteRule ^(.*/)$ #/$1 [R,L,NE]
And you will get the wanted redirect. The R flag plays together with the RewriteBase directive. Also the NE flag is necessary so that you can put # literally into the redirect URI.
The hash is already used by page anchor tags. You might need to replace it with an entity or pick a better character.
I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.