I'm looking for help on setting up routes in CakePHP3.4.6 where
urls are variable. For instance, I want the following urls:
/California/Posts/view/Skateboard/Jan2nd/10
/Texas/Posts/view/Truck/Feb10th/35
to connect to
/Posts/view/10
/Posts/view/35
respectively. When doing so, I need the URLs preserved in the browser.
(i.e. browser URL shows /California/Posts/view/Skateboard/Jan2nd/10 while content is served for /Posts/view/10)
Can this be done through configuring routes.php?
Any advice will be most appreciated.
I tried using rewrite rules in webroot/.htaccess such as:
RewriteRule ^[^/]+/Posts/view/[^/]+/[^/]+/(\d+)$ /Posts/view/$1 [L]
But this just ends up in 404 errors. The pattern match seems to be correct as the following rule works:
RewriteRule ^[^/]+/Posts/view/[^/]+/[^/]+/(\d+)$ http://www.google.com [L]
Thanks,
Managed to figure this out.
$routes->connect('/:state/Posts/view/:title/:date/:id',
['controller' => 'Posts', 'action' => 'view'],
['id' => '\d+', 'pass' => ['id']]
Did the job
Related
In my website, I am trying to rewrite few of my URLs as bellow:
This URL
http://example.com/news.php?newstype=newsletter&newsid=1123&newstitle=my news title
int to:
http://example.com/newsletter/1123/my news title
My rewrite rule in .htaccess looks like as shown below:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B]
My <a> tag is looks this:
$seoUrl = "$urlNewsType/$newsid/$urlNewsTitle/";
$seoUrl = urldecode($seoUrl);
$seoUrl = html_escape($seoUrl, 'UTF-8');
Read More
These every things are working for me. But my question is, I need to get these query string parameters separately from URL into PHP. Problem is I can't get these prameters as I do usually.
In php, This is the output of:
echo '<pre>',print_r($_GET).'</pre>';
Array
(
[newstype] => news/1114
[newsid] => Presentation: Heath+Day+in+2018
[newstitle] =>
)
Can anybody tell me, what is the wrong which I have done in this regard?
The regex pattern (.*) is greedy. It matches the full requested path. Try ([^/]+) instead :
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B]
How can I rewrite url from this:
https://www.domain.com/blog/category/sub-category-name
to this:
https://www.domain.com/blog/category/?category=sub-category-name
also it must work with others parameters like:
https://www.domain.com/blog/category/sub-category-name?param=value => https://www.domain.com/blog/category/?category=sub-category-name¶m=value
or
https://www.domain.com/blog/category/sub-category-name#hash => https://www.domain.com/blog/category/?category=sub-category-name#hash
I don't understand this rewrite syntax so any advice is valuable.
Thanks
You can use this rule :
RewriteEngine on
RewriteRule ^blog/([^/]+)/([^/]+)/?$ /blog/$1/?$1=$2 [QSA,NC,L]
Additional Query perameters are automatically appended to the target path by QSA flag.
I am trying to translate my URL, depending on the language selected by the user, e.g.:
www.example.com/en/discover or
www.example.com/fr/decouvrir
Obviously I want the two URL to point to the same page (where PHP handles the translation with a dictionary). I was wondering if there was a smart way in the htaccess file to do this "automatically" without having the write the rule for all the pages? For the moment I would do
RewriteRule ^en/discover$ discover.php?lang=en [NC,L]
RewriteRule ^fr/decouvrir$ discover.php?lang=fr [NC,L]
but this solution is not really the easiest if there are a lot of pages and if I want to add another language.
Thanks a lot for your help
You should pass all arguments to single php file (dispatcher), whitch would translate route and then include needed file. Something like:
.htaccess
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&include=$2 [NC,L]
PHP
// This is really bad code, just an example
$dictionary = array(
'en' => array(
'discover' => 'discover'
),
'fr' => array(
'decouvrir' => 'discover'
)
);
echo $dictionary[$_GET['lang']][$_GET['include']];
Hi I need some help with mod_rewrite.
I would like to change this kind of URL
127.0.0.1/app/controller/<anyName1>/<anyName2>.php?firstParam=1&secondParam=text&...&nParam=nSomething
to 127.0.0.1/anyName1/anyName2/1/text/.../nSomething
Could you give me any example how to do that?
You can save yourself a lot of stress by not trying to modify anything beyond /<anyName1>/<anyName2>/ with mod_rewrite. Dynamically mapping path components to query variables requires some very funky recursive voodoo magic.
Instead grab only /<anyName1>/<anyName2>/, append the rest of the path to the redirect, and then use PHP to parse the path:
.htaccess
RewriteCond %{REQUEST_URI} !^/app/controller/[^/]+/[^/]+\.php
RewriteRule ^([^/]+)/([^/]+)/?(.*)$ /app/controller/$1/$2.php/$3 [L]
PHP
$components = array_filter(explode('/', $_SERVER['PATH_INFO']), 'strlen');
echo '<pre>';
print_r($components);
echo '</pre>';
Output:
Array
(
[1] => 1
[2] => text
[3] => ...
[4] => nSomething
)
I was trying to rewrite a url in apache but it is not working as i expected. Here is how i defined in local environment
Allow from all
Satisfy Any
RewriteEngine On
RewriteOptions Inherit
RewriteRule ^(.*/)?email/?$ /api/v1/email [R=permanent,L]
I have defined this rewrite condition in mycompany.conf file which is located in the path
/etc/apache2/other
The url coming is "POST /items/50d42529dbc38e3d580002ff/email HTTP/1.1" 500 18750
I want to redirect /email to another path like /api/v1/email
I have defined a rails routes path for /items/50d42529dbc38e3d580002ff/email like this
match 'items/:id/email' => 'items#email', :as => :email_item, :via => :post
My question is based on rewrite rule it should go to api/v1/path and it shouldn't go to rails path that i defined in routes. It is not working like i thought.
I want to know whether my rewrite rule is correct or not and also i didn't define in .httaccess file. I included mycompany.conf file httpd.conf so i think it doesn't have a problem.
Please help me in this issue. Thanks in advance.