Mapping pages with htaccess - php

I have a php page and i'm trying to map it with htaccess. The idea is to map the page and if there are no parameters, the page is 1. this is for pagination.
My php page:
<?php echo $page = isset($_GET['p']) ? $_GET['p'] : 1; ?>
In my htaccess file i have this:
RewriteRule ^noticias/ noticias.php [L]
RewriteRule ^noticias/([0-9]+)/?$ noticias.php?p=$1 [L]
If i try to access the page with localhost/example/noticias.php and localhost/example/noticias.php?p=2 everything works.
If i add the first rule, the second rule does not work. And if i remove the first rule, the url wont work.
Tks in advance.

You may use these rules with MultiViews turned off:
Options -MultiViews
RewriteEngine on
RewriteRule ^(noticias)/?$ $1.php [NC,L]
RewriteRule ^(noticias)/(\d+)/?$ $1.php?p=$2 [QSA,NC,L]

Try this in your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^noticias/?$ noticias.php [QSA,NC,L]
RewriteRule ^noticias/([1-9]+)/?$ noticias.php?p=$1 [QSA,NC,L]
</IfModule>

Related

.htaccess URL Rewrite if a parameter is not exist

Below is the code which i use to url rewrite in our .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule pages/(.*)/? /page/index.php?name=$1 [NC,L]
from the above code if the url is www.example.com/pages/2 then actually it goes to url www.example.com/page/index.php?name=2
but also i want something like this : if a page request is www.example.com/pages/2/user/rahul then it should also go to same url www.example.com/page/index.php?name=2&user=rahul
so doing the above thing i did something like this :
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule pages/(.*)/? /page/index.php?name=$1 [NC,L]
RewriteRule pages/(.*)/user/(.*)/? /page/index.php?name=$1&user=$2 [NC,L]
but it does't work how i want.
Try :
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^pages/([^/]+)/user/([^/]+)/?$ /page/index.php?name=$1&user=$2 [NC,L]
RewriteRule ^pages/([^/]+)/?$ /page/index.php?name=$1 [NC,L]

Mod Rewrite htaccess rewriterule

I now have:
Order deny,allow
Options +FollowSymlinks
RewriteEngine on
DirectoryIndex index.php
RewriteRule ^en/newsevents$ ?action=setLang&lang=eng&menuid=6 [L,QSA]
It loads my texts but thiks it must look in the /en folder.
This does work, but changes the url from localhost/en/newsevents to localhost/name/?action=setLang&lang=eng&menuid=6
I want that it still remains localhost/en/newsevents
RewriteRule ^en/newsevents$ `http://localhost/name/?action=setLang&lang=eng&menuid=6 [L,QSA]`
Just do this:
RewriteRule ^(en/newsevents)$ $1?action=setLang&lang=eng&menuid=6 [QSA,L]
You need to use passthrough
RewriteRule ^en/newsevents$ ?action=setLang&lang=eng&menuid=6 [PT,L,QSA]

friendly url htaccess using variable

Target is to create friendly URL, and I got stuck here.
My link below:
Turpināt lasīt..
My .htaccess
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^blog/([0-9]+)/?$ blog.php?id=$1
No problems in configuration, simple rewrite rules work. What's wrong here?
Thanks
Ok try this code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+blog\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /blog/%1? [R=301,L]
RewriteRule ^blog/([0-9]+)/?$ /blog.php?id=$1 [L,QSA]

how to rewrite a url with two variables using .htaccess

i want rewrite it with a .htaccess i have this url:
../index.php?page=details&id=123456
like this:
../detail/123456
but I do not really know how to do this. Could you help me to rewrite this url?
or give me a simple example that I can understand how it works
RewriteRule ^(.*)/([0-9]+)$ index.php?page=$1&id=$2&%{QUERY_STRING}
The first (.*) selects any string, e.g. "details". ([0-9]+) catches a number, in your case "123456"
Finally
&%{QUERY_STRING}
ensures, that additional parameters are added to your backendscript, too.
This should work:
RewriteEngine On
RewriteRule ^detail/([^/]*)$ /index.php?page=details&id=$1 [L]
Here is an example I use for my webpage when it's in a subdirectory.
# "learn/degree/index.php?d=mathematics" shows as "learn/degree/mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
To complete it, remember to write at the beginning of the page this:
RewriteBase /
RewriteEngine On
Here's the 'full' .htaccess I use in my page to give you some idea.
#Redirect any www. to the page without it. Helps to keep user logged.
RewriteBase /
RewriteEngine On
rewriteCond %{HTTP_HOST} ^www.newfutureuniversity.org [NC]
rewriteRule ^(.*)$ http://newfutureuniversity.org/$1 [R=301,L]
Options +Indexes
Options +FollowSymlinks
# Finally working. Rewrite the user so "/student/Username" will internally be "/student/?user=Username"
RewriteRule ^student/([a-zA-Z0-9_-]+)$ student/index.php?user=$1
# Rewrite the disciplines so "learn/degree/1" will internally be "learn/degree/index.php?dis=1"
RewriteRule ^learn/degree/([1-4])$ learn/degree/index.php?dis=$1
# Rewrite the degrees so "learn/degree/mathematics" will internally be "learn/degree/index.php?d=mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
# Rewrite the degrees so "learn/subject/2/5/7" will internally be "learn/subject/index.php?class=2&div=5&section=7"
RewriteRule ^learn/subject/([0-9])$ learn/subject/index.php?class=$1
RewriteRule ^learn/subject/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2
RewriteRule ^learn/subject/([0-9])/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2&section=$3
You have to make the links to ../detail/123456, then the script interpretes it internally as ../index.php?page=details&id=123456. If you don't want this, then you are looking for a redirect.
Try this
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
# RewriteBase / add this if necessery, commented intentionally
RewriteRule ^detail/([0-9]+)$ index.php?page=details&id=$2
</IfModule>

apache rewrite static url to dynamic

I'm try to rewrite my url
http://www.domain.com/live/randomword
it should go rewrite to
http://www.domain.com/live/?cat=randomword
here are my tests:
RewriteRule ^live/(.*)$ /live/?cat=$1
RewriteRule ^live/(.*)$ live/?cat=$1
and all i have in the htaccess file is:
RewriteEngine on
You should try to add a RewriteBase / to your .htaccess and to suffix your rule with a [L] to say it's last rewrite rule ending with something like that:
RewriteEngine on
RewriteBase /
RewriteRule ^live/(.*)$ live/index.php?cat=$1 [L]
If this still doesn't work be sure to check that mod_rewrite is enabled
also you can do a [R,L] instead of [L] to see the redirection in the url and so have more info on what's going own.
hope this help
Have this code in your .htaccess file:
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^live/(.*)$ /live/?cat=$1 [L,NC]
in rewrite rule the file name or extension in missing?
write rule like,
RewriteRule ^live/([a-zA-Z0-9_-]+)$ /viewcategory.php?cat=$1

Categories