I have a single page index.php. I would like to accept parameters in the form /value. Right now, if I add anything after a /, it assumes I'm trying to access a separate file and errors out. Is there a way I could accept params by adding /value
Maby you can have a look at Silex.
This is a light weight framework and can do all your url desires.
You have to use apache's rewrite module. Check that rewrite_module is active.
Then you could place a simple .htaccess file in you directory with the following rules to do the job:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule (.*) index.php?value=$1 [L,NS]
This rules with activate rewrite engine and place everythin after / into value variable.
Have a look to the rewrite documentation and then adjust the rules to your own.
Related
I have a little mod_rewrite problem and really need some help.
I need to have a second .htaccess file in a subfolder for a project on my domain.
The URL to the project is like:
https://example.com/project-name/
Now I have a query parameter for which I want to create a nice URL with mod_rewrite.
The URL including the parameter looks like this:
https://example.com/project-name/index.php?preset=nameofapreset
And I want it to be:
https://example.com/project-name/nameofapreset
The important thing: The mod_rewrite has to work for the current folder only. Without having any info about the current path, cause I need to use the file in other folders, too.
That's what I thought should work (cause I am using a similar rewrite rule in another project):
RewriteEngine On
RewriteRule ^([a-z])/?$ index.php?preset=$1
But it doesn't. And all my trials fail.
Any ideas how I can make it work?
Thanks!
You can use this rule in /project-name/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?preset=$1 [L,QSA]
I am working on a Wordpress site, it requires a plugin that needs to access the API endpoint of WP.
It wants to access the site's:
http://myhost.test/wp/wp-json/erp/v1/hrm/employees/1\?include\=department,designation,reporting_to,avatar,roles
but changes to the folder structure, causes the real, and actual link should be:
http://myhost.test/wp-json/erp/v1/hrm/employees/1\?include\=department,designation,reporting_to,avatar,roles
as you can see, there should be no /wp at the start of the %{REQUEST_URI}
I am trying to make this work: but it won't redirect the request:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/wp/wp\-json/
RewriteRule ^/wp/wp\-json(.*) /wp\-json$1 [L,R]
I do not understand what I am doing wrong,
I am catching everything after ^/wp/wp-json then forward it to ^/wp-json
What is going on?
Regards,
Using mod_rewrite in .htaccess context vs a <Directory> or <VirtualHost> context each have slightly different requirements in how the patterns are parsed. Most importantly to your situation, in .htaccess the first argument to RewriteRule is not matched against a leading slash / because the pattern is considered relative to the directory it is in.
Remove the leading / from your RewriteRule matcher argument:
RewriteRule ^wp/wp\-json(.*) /wp\-json$1 [L,R]
I am using this rule to rewrite the link
RewriteEngine On
RewriteRule ^(.*)/(.*) show_cv.php?email=$1
It is working fine like if I write this url with last slash
www.mysite.com/letschat_2008#yahoo.com/ ----> index.php?email=letschat_2008#yahoo.com
But when I remove the last slash from the link www.mysite.com/letschat_2008#yahoo.com/ it shows error 404.
I wish the URL Rewrite rule would work for both with slash and without slash (/)
www.mysite.com/letschat_2008#yahoo.com/ ----> index.php?email=letschat_2008#yahoo.com
www.mysite.com/letschat_2008#yahoo.com ----> index.php?email=letschat_2008#yahoo.com
Your rules are looping, you need to make sure you are rewriting an email address, and add some conditions so that the rule doesn't get applied if it's accessing an existing resource:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_\-\#\.]+)/?$ /show_cv.php?email=$1 [L]
You should then be using the following rule:
RewriteRule ^(.*)/? show_cv.php?email=$1
I assume you note these rules in a .htaccess file, not in the server configuration when looking at your description ?
Rethink if you don-t want to put this into the server configuration. Apart from the usage of .htaccess files being harder to debug using rewrite rules in those files is more complex than in the server configuration. This is documented in mod_rewrites docs.
The reason for the behaviour is the different content of REQUEST_URI in both cases. Have a try checking this directly and you will see the problem. The whole part "letschat_2008#yahoo.com" is simply missing in that variable in case 2 (no "/"). To get this working you must use an additional rewriteCondition (also documented...). Something like these untested lines:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)&
RewriteRule - show_cv.php?email=%1
(note the '%' instead of a '$' in the last line)
I need a little help figuring out what the following URL rewrite rule means. I can understant the first three lines, but I got stuck with the index.php/$1 part. What does exactly the / means in this rule? The only thing I would always expect to see after a file name would be a query-string separator (?). This is the first time I am seeing the / as a separator. What does it exactly mean?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
The <IfModule mod_rewrite.c>...</IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.
The following two lines are conditions for the RewriteRule line which follows them. It means that the RewriteRule will be evaluated only if these two conditions are met.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These lines simply state that rewriting (RewriteRule line) will occur only if there are no existing files or folders on the server which match the URI. If they do exist then they will be served instead, unless there is some other directive that prevents it, otherwise rewriting will occur.
The last line will do the actual rewriting. It will take whatever is following the website domain name and append it to a rewritten request which will begin with index.php/.
Here is an example.
Lets say you make a request for example.com/example-page.html.
If there is no existing file or folder in the virtual hosts root folder named example-page.html the rewrite rule at the end will rewrite the request to look like example.com/index.php/example-page.html.
The main reason why applications rewrite requests like this is to ensure that they have a single point of entry, often called a bootstrap, which is considered to be a good practice from the security standpoint and also offers more granular control of requests.
Here is in my opinion a very good beginner friendly tutorial for mod_rewrite.
It's just rewritting the url name.
For example, this url:
http://www.example.com/something/else
Will be the same as:
http://www.example.com/index.php/something/else
The main navigation of my site is coded like this:
<li>'.$value.'</li>'."\n";
But I would like the URL of the links to look like domain.co.nz/pagename, not domain.co.nz/index.php?pageId=pagename
How would you recommend I accomplish this?
Something like this should work:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?pageID=$1
First line turns on mod_rewrite. Second line sets the base URL to / (it's annoying, but you have to set it to the base path you're dealing with). Third and fourth lines make sure the request doesn't exist as a file, or as a directory. And the last line is the actual magic; basically it searches for "anything", captures what it finds in $1, and "rewrites" the URL to index.php?pageID=$1. If you learn to use regexes, you can do much more complicated things as well.
Yes, you can accomplish this with a .htaccess RewriteRule. In your .htaccess file, include:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?pageId=$1
This means:
If the REQUEST_FILENAME is not a valid file, redirect the entire URL to index.php?pageId=the entire URL
You'd then change your navigation to:
echo "<li>'.$value.'</li>'."\n";
Edit: I moved Trivikrtam's edit inline, see above. The RewriteRule should be index.php?pageId=$1 not /index.php?pageId=$1. Thanks #Trivikrtam!