so I have a few different links on my website, and I am looking for some advice on how I can achieve this.
Some example links:
domain/index.php?page=commandCenter&ID=40
domain/index.php?page=view&action=edit&ID=40
domain/index.php?page=acpDashboard
I would like the rewrite to look like the following for each link:
domain/commandCenter/40
domain/view/edit/40
domain/acpDashboard
Though it seems in order for this to work, I need to have something in place for those variables.
The last link would not work, but
domain/acpDashboard/0/0
is what it needs to work.
Is this possible, and how would I go about it? I only have 3 different variables page, action, and ID.
Here is my htaccess atm!
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /index.php? page=$1&action=$2&ID=$3 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thanks for any advice!
Your here is wrong you can reference .htaccess RewriteRule to preserve GET URL parameters this answer
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /index.php?page=$1&action=$2&ID=$3 [L]
Related
I honestly can't see where I'm going wrong with mod_rewrite. I'm wanting to build a basic social network, but I want the user profile URLs to look like subdomains, like "www.mysite.com/user/fred" and not "www.mysite.com/user?u=fred". My .htaccess page looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
RewriteRule ^user/([A-Za-z]+)/(0-9]+)/? user?u=$1 [L,QSA]
But navigating to www.mysite.com/user/fred gives me a 404 error. I've tried a few different ones from different tutorial websites and stackoverflow questions, but none of them will work. I know that mod_rewrite is definitely installed on my server, because I checked the php_info() and the rewrite rule for normal URLs work fine ("www.mysite.com/browse" instead of "www.mysite.com/browse.php").
Try replacing your 3rd and 4th line for:
RewriteRule ^user/(.*) user.php?u=$1 [QSA]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have a working RewriteRule that rewrites any page to the literal url:
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
The problem is I want to add page=$1&category=$2 and convert it to...obviously... /category/page
I tried this, but it doesn't seem to work:
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
I should note that I would still like to be able to access pages without categories - ie /login or /about that should go to index.php?page=about for instance
Your solution will be as below.
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?page=$1&category=$2 [NC,L]
Input url : http://www.test.com/my-cat/my-page
and it will be treated as : http://www.test.com/index.php?page=my-cat&category=my-page
Try with below, we are instructing apache to don't look for directory or file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
try this
#https://www.example.com/index.php?category=9&page=CBSE `#https://www.example.com/category/page/CBSE`
Method One:
#use this code for generate new url
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2 [NC,L]
Method Two :
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2
Note :Hit Your Url index.php?category=$1&page=$2 to convert $i & $2 Create Dynamic Url Your Id Bases
This will work for you.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&page=$2 [L]
If you want to generate rewrite urls easily, there are a lot online rewrite url generators.
Thanks for all the contributions...
Because I needed to cater for both /page and /category/page...I ended up doing this:
RewriteRule ^([0-9a-zA-Z_\-][^/]+)$ index.php?page=$1 [N]
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?category=$1&page=$2 [L]
But that was only half the solution as I needed to modify my PHP to check whether category is set or not and to send a 404 if it doesn't match. Otherwise /invalid_category/valid_pagewould still work.
The ideas above all pushed me in the right direction, thanks.
I need to have URLs fitting this criteria:
www.domain.com/abc
rewritten to be:
www.domain.com/index.php?name=abc
I can do it where the last letter(s) are fixed like this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*).htm$ index.php?name=$1
which means www.domain.com/abc.htm would go to www.domain.com/index.php?name=abc
but I need it to not have the .htm (or anything after the abc)
Can this be done? I've spent some time trying to find the solution but so far without success.
Thanks in anticipation.
Jon
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) /index.php?name=$1 [L]
I usually use RewriteRule ^(.*)$ index.php?name=$1
I am not completely sure on your criteria though, this will send everything to the PHP script.
If you only want to rewrite the first part of a path ie. /abc but not abc/def or abc/ I can change it for you.
Okey, so this is my problem.
I want to use mod_rewrite to make nice looking urls for my site.
I want them all to have good looking url like www.mypage/tennis or www.mypage/soccer instead of www.mypage/?page=tennis and www.mypage/?page=soccer
And with the following rules i can achive this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]
So now if I type in www.mypage/soccer my PHP script reads this url and does it's magic translating this to $_GET['page'] = soccer. All this works fine!
Problem is if I would type the URL www.mypage/soccer/ all of a sudden every linked css or image cannot be found by the website, since it now looking in the none existing folder /soccer/ off course.
How do I make a rewrite rule that transforms /soccer/ to /soccer or any other /blabla/ to /blabla
Hope my question is clear! Also if anyone have any good pages where I can learn more regular expressions i would be very happy!
Try this:
RewriteRule ^([A-Za-z0-9-]+)/?$ ?page=$1 [L]
I think you should not use mod_rewrite for this, but rather fix your CSS and image paths.
Change the paths from relative to absolute,
meaning, the paths should begin with a /.
If you have this, no matter on which site your are, /images/myimage.png will always refer to www.mypage.com/images/myimage.png.
# For URIs with query string:
RewriteCond %{QUERY_STRING} (.+)
RewriteRule (.*)/$ ?page=$1&%1 [L]
# All other
RewriteRule (.*)/$ ?page=$1 [L]
Hey all, how can I reduce this:
www.example.com/index.php?page=viewblog&category=awesome
down to this:
www.example.com/blog/awesome
The above lists all of the blog posts in that category, but I also want scope for adding the title of the post on the end of it as well, like this:
www.example.com/index.php?page=viewblog&category=awesome&post=why-trees-are-green
And this needs to shorten down to:
www.example.com/blog/awesome/why-trees-are-green
Any ideas, anyone? Thanks in advance :)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule blog/([^/]+)/?$ index.php?page=viewblog&category=$1 [L]
RewriteRule blog/([^/]+)/([^/]+)/?$ index.php?page=viewblog&category=$1&post=$2 [L]
The condition makes sure the requested URL is not an actual file, which you want to serve (css, images, etc.)
Then, one rule per level, the first one will rewrite
http://yourserver.com/blog/whatever to
http://yourserver.com/index.php?page=viewblog&category=whatever
The second one will rewrite http://yourserver.com/blog/whatever/whenever to
http://yourserver.com/index.php?page=viewblog&category=whatever&post=whenever
If you need more levels, add more rules accordingly.
Something like this?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/([^./]+)$ index.php?page=view$1&category=$2 [L]
RewriteRule ^([^./]+)/([^./]+)/([^./]+)$ index.php?page=view$1&category=$2&post=$3 [L]